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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/// Assert that a type or value implements a given list of traits at compile time.
#[macro_export(local_inner_macros)]
macro_rules! const_assert_impl {
    ($val:ident: $tr:path) => {{
        const fn value_must_impl<T: $tr>(_: &T) {}
        value_must_impl(&$val);
    }};
    ($val:ident: $tr:path, $($rest:path),+) => {
        const_assert_impl!($val: $tr);
        const_assert_impl!($val: $($rest),+);
    };
    ($ty:ty: $tr:path) => {{
        const fn type_must_impl<T: $tr>() {}
        type_must_impl::<$ty>();
    }};
    ($ty:ty: $tr:path, $($rest:path),+) => {
        const_assert_impl!($ty: $tr);
        const_assert_impl!($ty: $($rest),+);
    };
    ({$val:expr}: $($tr:path),+) => {{
        let tmp = $val;
        const_assert_impl!(tmp: $($tr),+)
    }};
}

/// Ensure the alignment of a type is a certain value at compile time.
#[macro_export(local_inner_macros)]
macro_rules! const_assert_align {
    (?const_expr ($x:expr)) => {
        const _: [(); 0 - !{ const ASSERT: bool = $x; ASSERT } as usize] = [];
    };
    ($ty:ty, $align:expr) => {{
        const_assert_align!(?const_expr (::std::mem::align_of::<$ty>() == $align));
    }};
}

/// The type must be `Copy`. Creates an aligned copy of a field on the stack for easy reading.
#[macro_export(local_inner_macros)]
macro_rules! packed_read {
    ($val:ident.$field:ident) => {{
        const_assert_impl!({$val.$field}: ::std::marker::Copy);

        let brw = ::std::ptr::addr_of!($val.$field);
        unsafe { brw.read_unaligned() }
    }};
    (($expr:expr).$field:ident) => {{
        let tmp = $expr;
        packed_read!(tmp.$field)
    }};
}

/// The type must be `Copy`. Creates an aligned copy of a field on the stack for easy reading.
#[macro_export(local_inner_macros)]
macro_rules! unaligned_read {
    ($val:ident) => {{
        let brw = ::std::ptr::addr_of!(*$val);
        let alg = unsafe { brw.read_unaligned() };
        const_assert_impl!(alg: ::std::marker::Copy);
        alg
    }};
    ($expr:expr) => {{
        let tmp = $expr;
        unaligned_read!(tmp)
    }};
}

/// Reads a possibly unaligned pointer as a reference.
#[macro_export]
macro_rules! unaligned_ref_read {
    ($ptr:ident as $ty:ty) => {{
        debug_assert!(!$ptr.is_null());
        let r: $ty = unsafe { $ptr.read_unaligned() };
        r
    }};
}

/// Reads a possibly unaligned pointer as a value and passes it to a closure.
#[macro_export]
macro_rules! unaligned_do {
    ($ptr:ident as $ty:ty => |$arg:ident| $closure:expr) => {{
        debug_assert!(!$ptr.is_null());
        let $arg: $ty = unsafe { $ptr.read_unaligned() };
        let _returned = $closure;
        // we must FORGET this because `read_unaligned` makes a shallow copy.
        ::std::mem::forget($arg);
        _returned
    }};
}

/// Serialize values from a packed type.
/// Use `fn _serialize_chained` to generate a chain serialization function for use within
/// `SubRecord`, or just pass the type, value, and field to chain serialize a given value.
#[macro_export(local_inner_macros)]
macro_rules! define_serialize_chained {
    // It is a fat pointer type (e.g. &str or &[u8])
    (&$ty:ty => |$zelf:ident, $dest:ident| $closure:expr) => {
        #[inline]
        fn _serialize_chained<W: Write>(&self, dest: &mut W) -> $crate::SeResult<usize> {
            let $zelf: &$ty = self;
            let $dest = dest;
            $closure
        }

        #[inline]
        unsafe fn _serialize_chained_unaligned<W: ::std::io::Write>(zelf: *const Self, dest: &mut W) -> $crate::SeResult<usize> {
            let $zelf: &$ty = unaligned_ref_read!(zelf as &$ty);
            let $dest = dest;
            $closure
        }
    };
    // It is a copy type
    (*$ty:ty => |$zelf:ident, $dest:ident| $closure:expr) => {
        #[inline]
        fn _serialize_chained<W: ::std::io::Write>(&self, dest: &mut W) -> $crate::SeResult<usize> {
            let $zelf: $ty = *self;
            let $dest = dest;
            $closure
        }

        #[inline]
        unsafe fn _serialize_chained_unaligned<W: ::std::io::Write>(zelf: *const Self, dest: &mut W) -> $crate::SeResult<usize> {
            let $zelf: $ty = unaligned_read!(zelf);
            let $dest = dest;
            $closure
        }
    };
    // It is not a fat pointer and it is not Copy
    ($ty:ty => |$zelf:ident, $dest:ident| $closure:expr) => {
        #[inline]
        fn _serialize_chained<W: ::std::io::Write>(&self, dest: &mut W) -> $crate::SeResult<usize> {
            let $zelf: &$ty = self;
            let $dest = dest;
            $closure
        }

        #[inline]
        unsafe fn _serialize_chained_unaligned<W: ::std::io::Write>(zelf: *const Self, dest: &mut W) -> $crate::SeResult<usize> {
            let $dest = dest;
            unaligned_do!(zelf as $ty => |$zelf| $closure)
        }
    };
}