summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/bitcast.rs
blob: 77e0e6338afdde1d3b1823b8a809432e2aa79804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//! The `bitcast` and `bitflags_bits` macros.

#![allow(unused_macros)]

// Ensure that the source and destination types are both primitive integer
// types and the same size, and then bitcast.
macro_rules! bitcast {
    ($x:expr) => {{
        if false {
            // Ensure the source and destinations are primitive integer types.
            let _ = !$x;
            let _ = $x as u8;
            0
        } else if false {
            // Ensure that the source and destinations are the same size.
            // SAFETY: This code is under an `if false`.
            #[allow(unsafe_code, unused_unsafe, clippy::useless_transmute)]
            unsafe {
                ::core::mem::transmute($x)
            }
        } else {
            // Do the conversion.
            $x as _
        }
    }};
}

/// Return a [`bitcast`] of the value of `$x.bits()`, where `$x` is a
/// `bitflags` type.
macro_rules! bitflags_bits {
    ($x:expr) => {{
        bitcast!($x.bits())
    }};
}