rustix/
bitcast.rs

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.
7macro_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            // SAFETY: This code is under an `if false`.
17            #[allow(unsafe_code, unused_unsafe, clippy::useless_transmute)]
18            unsafe {
19                ::core::mem::transmute($x)
20            }
21        } else {
22            // Do the conversion.
23            $x as _
24        }
25    }};
26}
27
28/// Return a [`bitcast`] of the value of `$x.bits()`, where `$x` is a
29/// `bitflags` type.
30macro_rules! bitflags_bits {
31    ($x:expr) => {{
32        bitcast!($x.bits())
33    }};
34}