rend/
util.rs

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
macro_rules! match_endian {
    (little $little:expr, $big:expr $(,)?) => {
        $little
    };
    (big $little:expr, $big:expr $(,)?) => {
        $big
    };
}

macro_rules! if_native_endian {
    ($endian:ident $true:expr, $false:expr $(,)?) => {
        match_endian!(
            $endian
            {
                #[cfg(target_endian = "little")]
                {
                    $true
                }
                #[cfg(target_endian = "big")]
                {
                    $false
                }
            },
            {
                #[cfg(target_endian = "little")]
                {
                    $false
                }
                #[cfg(target_endian = "big")]
                {
                    $true
                }
            },
        )
    }
}

macro_rules! swap_endian {
    ($endian:ident $expr:expr) => {
        if_native_endian!($endian $expr, $expr.swap_bytes())
    }
}

macro_rules! endian_name {
    ($endian:ident) => {
        match_endian!($endian "little", "big")
    };
}

#[cfg(any(
    target_has_atomic = "16",
    target_has_atomic = "32",
    target_has_atomic = "64",
))]
macro_rules! opposite_endian_name {
    ($endian:ident) => {
        match_endian!($endian "big", "little")
    };
}

#[cfg(test)]
macro_rules! assert_size_align {
    ($($name:ident $size:literal $align:literal),* $(,)?) => {
        $(
            assert_eq!(core::mem::size_of::<$name>(), $size);
            assert_eq!(core::mem::align_of::<$name>(), $align);
        )*
    }
}