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(
18 unsafe_code,
19 unused_unsafe,
20 clippy::useless_transmute,
21 clippy::missing_transmute_annotations
22 )]
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.
35macro_rules! bitflags_bits {
36 ($x:expr) => {{
37 bitcast!($x.bits())
38 }};
39}