xref: /qemu/rust/qemu-api/src/assertions.rs (revision 8ed7c0b6488a7f20318d6ba414f1cbcd0ed92afe)
1 // Copyright 2024, Red Hat Inc.
2 // Author(s): Paolo Bonzini <pbonzini@redhat.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 #![doc(hidden)]
6 //! This module provides macros to check the equality of types and
7 //! the type of `struct` fields.  This can be useful to ensure that
8 //! types match the expectations of C code.
9 //!
10 //! Documentation is hidden because it only exposes macros, which
11 //! are exported directly from `qemu_api`.
12 
13 // Based on https://stackoverflow.com/questions/64251852/x/70978292#70978292
14 // (stackoverflow answers are released under MIT license).
15 
16 #[doc(hidden)]
17 pub trait EqType {
18     type Itself;
19 }
20 
21 impl<T> EqType for T {
22     type Itself = T;
23 }
24 
25 /// Assert that two types are the same.
26 ///
27 /// # Examples
28 ///
29 /// ```
30 /// # use qemu_api::assert_same_type;
31 /// # use std::ops::Deref;
32 /// assert_same_type!(u32, u32);
33 /// assert_same_type!(<Box<u32> as Deref>::Target, u32);
34 /// ```
35 ///
36 /// Different types will cause a compile failure
37 ///
38 /// ```compile_fail
39 /// # use qemu_api::assert_same_type;
40 /// assert_same_type!(&Box<u32>, &u32);
41 /// ```
42 #[macro_export]
43 macro_rules! assert_same_type {
44     ($t1:ty, $t2:ty) => {
45         const _: () = {
46             #[allow(unused)]
47             fn assert_same_type(v: $t1) {
48                 fn types_must_be_equal<T, U>(_: T)
49                 where
50                     T: $crate::assertions::EqType<Itself = U>,
51                 {
52                 }
53                 types_must_be_equal::<_, $t2>(v);
54             }
55         };
56     };
57 }
58 
59 /// Assert that a field of a struct has the given type.
60 ///
61 /// # Examples
62 ///
63 /// ```
64 /// # use qemu_api::assert_field_type;
65 /// pub struct A {
66 ///     field1: u32,
67 /// }
68 ///
69 /// assert_field_type!(A, field1, u32);
70 /// ```
71 ///
72 /// Different types will cause a compile failure
73 ///
74 /// ```compile_fail
75 /// # use qemu_api::assert_field_type;
76 /// # pub struct A { field1: u32 }
77 /// assert_field_type!(A, field1, i32);
78 /// ```
79 #[macro_export]
80 macro_rules! assert_field_type {
81     (@internal $param_name:ident, $ti:ty, $t:ty, $($field:tt)*) => {
82         const _: () = {
83             #[allow(unused)]
84             fn assert_field_type($param_name: &$t) {
85                 fn types_must_be_equal<T, U>(_: &T)
86                 where
87                     T: $crate::assertions::EqType<Itself = U>,
88                 {
89                 }
90                 types_must_be_equal::<_, $ti>(&$($field)*);
91             }
92         };
93     };
94 
95     ($t:ty, $i:tt, $ti:ty) => {
96         $crate::assert_field_type!(@internal v, $ti, $t, v.$i);
97     };
98 
99     ($t:ty, $i:tt, $ti:ty, num = $num:ident) => {
100         $crate::assert_field_type!(@internal v, $ti, $t, v.$i[0]);
101     };
102 }
103 
104 /// Assert that an expression matches a pattern.  This can also be
105 /// useful to compare enums that do not implement `Eq`.
106 ///
107 /// # Examples
108 ///
109 /// ```
110 /// # use qemu_api::assert_match;
111 /// // JoinHandle does not implement `Eq`, therefore the result
112 /// // does not either.
113 /// let result: Result<std::thread::JoinHandle<()>, u32> = Err(42);
114 /// assert_match!(result, Err(42));
115 /// ```
116 #[macro_export]
117 macro_rules! assert_match {
118     ($a:expr, $b:pat) => {
119         assert!(
120             match $a {
121                 $b => true,
122                 _ => false,
123             },
124             "{} = {:?} does not match {}",
125             stringify!($a),
126             $a,
127             stringify!($b)
128         );
129     };
130 }
131 
132 /// Assert at compile time that an expression is true.  This is similar
133 /// to `const { assert!(...); }` but it works outside functions, as well as
134 /// on versions of Rust before 1.79.
135 ///
136 /// # Examples
137 ///
138 /// ```
139 /// # use qemu_api::static_assert;
140 /// static_assert!("abc".len() == 3);
141 /// ```
142 ///
143 /// ```compile_fail
144 /// # use qemu_api::static_assert;
145 /// static_assert!("abc".len() == 2); // does not compile
146 /// ```
147 #[macro_export]
148 macro_rules! static_assert {
149     ($x:expr) => {
150         const _: () = assert!($x);
151     };
152 }
153