1 | //! The `bitcast` and `bitflags_bits` macros. |
2 | |
3 | #![allow (unused_macros)] |
4 | |
5 | // Ensure that the source and destination types are both primitive integer |
6 | // types and the same size, and then bitcast. |
7 | macro_rules! bitcast { |
8 | ($x:expr) => {{ |
9 | if false { |
10 | // Ensure the source and destinations are primitive integer types. |
11 | let _ = !$x; |
12 | let _ = $x as u8; |
13 | 0 |
14 | } else if false { |
15 | // Ensure that the source and destinations are the same size. |
16 | #[allow( |
17 | unsafe_code, |
18 | unused_unsafe, |
19 | clippy::useless_transmute, |
20 | clippy::missing_transmute_annotations |
21 | )] |
22 | // SAFETY: This code is under an `if false`. |
23 | unsafe { |
24 | ::core::mem::transmute($x) |
25 | } |
26 | } else { |
27 | // Do the conversion. |
28 | $x as _ |
29 | } |
30 | }}; |
31 | } |
32 | |
33 | /// Return a [`bitcast`] of the value of `$x.bits()`, where `$x` is a |
34 | /// `bitflags` type. |
35 | macro_rules! bitflags_bits { |
36 | ($x:expr) => {{ |
37 | bitcast!($x.bits()) |
38 | }}; |
39 | } |
40 | |