linearize/
impls.rs

1// NOTE: This macro exists because LLVM will not properly optimize from_linear_unchecked
2// implementations that use the hint directly as their terminating branch.
3macro_rules! cold_unreachable {
4    () => {{
5        #[cold]
6        unsafe fn unreachable() -> ! {
7            unsafe {
8                core::hint::unreachable_unchecked();
9            }
10        }
11        unreachable()
12    }};
13}
14
15macro_rules! impl_assert {
16    ($ty:ty) => {
17        #[cfg(test)]
18        static_assertions::assert_type_eq_all! {
19            <$ty as $crate::Linearize>::Storage<u8>,
20            <$ty as $crate::Linearize>::CopyStorage<u8>,
21            [u8; <$ty as $crate::Linearize>::LENGTH],
22        }
23    };
24    ($ty:ty, $length:expr) => {
25        impl_assert!($ty);
26
27        #[cfg(test)]
28        static_assertions::const_assert_eq! {
29            <$ty as $crate::Linearize>::LENGTH,
30            $length,
31        }
32    };
33}
34
35#[cfg(test)]
36fn test_delinearize<T: crate::Linearize>(_base: &T, v: usize) -> T {
37    unsafe { T::from_linear_unchecked(v) }
38}
39
40#[cfg(test)]
41macro_rules! assert_roundtrip {
42    ($t:expr) => {{
43        let t = $t;
44        let l = t.linearize();
45        assert_eq!(crate::impls::test_delinearize(&t, l), t);
46    }};
47    ($t:expr, $l:expr) => {{
48        let t = $t;
49        let l = t.linearize();
50        assert_eq!(l, $l);
51        assert_eq!(crate::impls::test_delinearize(&t, l), t);
52    }};
53}
54
55mod bool;
56mod enums;
57mod infallible;
58mod integers;
59mod phantom_data;
60mod phantom_pinned;
61mod unit;