xref: /openbmc/qemu/rust/common/src/assertions.rs (revision ccafa85a97e38698b798115bba6c18c849846e25)
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 `common`.
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 common::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 common::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 common::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 common::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             const fn assert_field_type($param_name: &$t) {
85                 const 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 
100 /// Assert that an expression matches a pattern.  This can also be
101 /// useful to compare enums that do not implement `Eq`.
102 ///
103 /// # Examples
104 ///
105 /// ```
106 /// # use common::assert_match;
107 /// // JoinHandle does not implement `Eq`, therefore the result
108 /// // does not either.
109 /// let result: Result<std::thread::JoinHandle<()>, u32> = Err(42);
110 /// assert_match!(result, Err(42));
111 /// ```
112 #[macro_export]
113 macro_rules! assert_match {
114     ($a:expr, $b:pat) => {
115         assert!(
116             match $a {
117                 $b => true,
118                 _ => false,
119             },
120             "{} = {:?} does not match {}",
121             stringify!($a),
122             $a,
123             stringify!($b)
124         );
125     };
126 }
127 
128 /// Assert at compile time that an expression is true.  This is similar
129 /// to `const { assert!(...); }` but it works outside functions, as well as
130 /// on versions of Rust before 1.79.
131 ///
132 /// # Examples
133 ///
134 /// ```
135 /// # use common::static_assert;
136 /// static_assert!("abc".len() == 3);
137 /// ```
138 ///
139 /// ```compile_fail
140 /// # use common::static_assert;
141 /// static_assert!("abc".len() == 2); // does not compile
142 /// ```
143 #[macro_export]
144 macro_rules! static_assert {
145     ($x:expr) => {
146         const _: () = assert!($x);
147     };
148 }
149