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
/// Numeric-like type can be represented by a `u64`.
///
/// Used by `Atomic` to store values.
pub(crate) trait Numeric: Sized + Copy + PartialEq {
    /// Convert a value into `u64` representation
    fn into_u64(self) -> u64;

    /// Convert a `u64` representation into the value
    fn from_u64(src: u64) -> Self;
}

macro_rules! impl_num {
    ( $($t:ty),* ) => {
        $(
            impl Numeric for $t {
                fn into_u64(self) -> u64 {
                    self as u64
                }

                fn from_u64(src: u64) -> $t {
                    src as $t
                }
            }
        )*
    };
}

impl_num!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);

impl<T> Numeric for *mut T {
    fn into_u64(self) -> u64 {
        self as u64
    }

    fn from_u64(src: u64) -> *mut T {
        src as *mut T
    }
}

impl Numeric for bool {
    fn into_u64(self) -> u64 {
        if self {
            1
        } else {
            0
        }
    }

    fn from_u64(src: u64) -> bool {
        src != 0
    }
}