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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use core::{borrow::Borrow, mem::size_of};

use crate::{
    buffer::Buffer,
    deserialize::{Deserialize, DeserializeError, Deserializer},
    formula::{BareFormula, Formula},
    serialize::{write_bytes, Serialize, SerializeRef, Sizes},
};

macro_rules! impl_primitive {
    () => {};

    ([$($head:ident)+] $([$($tail:ident)+])*) => {
        impl_primitive!(@ < $($head)+);
        impl_primitive!($([$($tail)+])*);
    };

    (@ $($head:ident)* <) => {};
    (@ $($head:ident)* < $cursor:ident $($tail:ident)*) => {
        impl_primitive!{! $($head)* < $cursor < $($tail)* }
        impl_primitive!{@ $($head)* $cursor < $($tail)* }
    };

    (! $($from:ident)* < $ty:ident < $($to:ident)*) => {
        impl Formula for $ty {
            const MAX_STACK_SIZE: Option<usize> = Some(size_of::<$ty>());
            const EXACT_SIZE: bool = true;
            const HEAPLESS: bool = true;
        }

        impl BareFormula for $ty {}

        impl Serialize<$ty> for $ty {
            #[inline(always)]
            fn serialize<B>(self, sizes: &mut Sizes, buffer: B) -> Result<(), B::Error>
            where
                B: Buffer,
            {
                write_bytes(&self.to_le_bytes(), sizes, buffer)
            }

            #[inline(always)]
            fn size_hint(&self) -> Option<Sizes> {
                Some(Sizes{ heap: 0, stack: size_of::<$ty>()})
            }
        }

        $(
            impl Serialize<$ty> for $from {
                #[inline(always)]
                fn serialize<B>(self, sizes: &mut Sizes, buffer: B) -> Result<(), B::Error>
                where
                    B: Buffer,
                {
                    write_bytes(&$ty::from(self).to_le_bytes(), sizes, buffer)
                }

                #[inline(always)]
                fn size_hint(&self) -> Option<Sizes> {
                    Some(Sizes{ heap: 0, stack: size_of::<$ty>()})
                }
            }
        )*

        impl SerializeRef<$ty> for $ty {
            #[inline(always)]
            fn serialize<B>(&self, sizes: &mut Sizes, buffer: B) -> Result<(), B::Error>
            where
                B: Buffer,
            {
                write_bytes(&self.to_le_bytes(), sizes, buffer)
            }

            #[inline(always)]
            fn size_hint(&self) -> Option<Sizes> {
                Some(Sizes{ heap: 0, stack: size_of::<$ty>()})
            }
        }

        $(
            impl SerializeRef<$ty> for $from {
                #[inline(always)]
                fn serialize<B>(&self, sizes: &mut Sizes, buffer: B) -> Result<(), B::Error>
                where
                    B: Buffer,
                {
                    write_bytes(&$ty::from(*self).to_le_bytes(), sizes, buffer)
                }

                #[inline(always)]
                fn size_hint(&self) -> Option<Sizes> {
                    Some(Sizes{ heap: 0, stack: size_of::<$ty>()})
                }
            }
        )*

        impl<T> Deserialize<'_, $ty> for T
        where
            T: From<$ty>,
        {
            #[inline(always)]
            fn deserialize(mut de: Deserializer) -> Result<Self, DeserializeError> {
                let input = de.read_byte_array::<{size_of::<$ty>()}>()?;
                // de.finish()?;
                let value = <$ty>::from_le_bytes(input);
                return Ok(From::from(value));
            }

            #[inline(always)]
            fn deserialize_in_place(&mut self, mut de: Deserializer) -> Result<(), DeserializeError> {
                let input = de.read_byte_array::<{size_of::<$ty>()}>()?;
                // de.finish()?;
                let value = <$ty>::from_le_bytes(input);
                *self = From::from(value);
                Ok(())
            }
        }
    };
}

impl_primitive! {
    [u8 u16 u32 u64 u128]
    [i8 i16 i32 i64 i128]
    [f32 f64]
}

impl Formula for bool {
    const MAX_STACK_SIZE: Option<usize> = Some(1);
    const EXACT_SIZE: bool = true;
    const HEAPLESS: bool = true;
}

impl BareFormula for bool {}

impl Serialize<bool> for bool {
    #[inline(always)]
    fn serialize<B>(self, sizes: &mut Sizes, buffer: B) -> Result<(), B::Error>
    where
        Self: Sized,
        B: Buffer,
    {
        write_bytes(&[u8::from(self)], sizes, buffer)
    }

    #[inline(always)]
    fn size_hint(&self) -> Option<Sizes> {
        Some(Sizes {
            heap: 0,
            stack: size_of::<u8>(),
        })
    }
}

impl Serialize<bool> for &bool {
    #[inline(always)]
    fn serialize<B>(self, sizes: &mut Sizes, buffer: B) -> Result<(), B::Error>
    where
        B: Buffer,
    {
        <u8 as Serialize<u8>>::serialize(u8::from(*self.borrow()), sizes, buffer)
    }

    #[inline(always)]
    fn size_hint(&self) -> Option<Sizes> {
        Some(Sizes {
            heap: 0,
            stack: size_of::<u8>(),
        })
    }
}

impl<T> Deserialize<'_, bool> for T
where
    T: From<bool>,
{
    #[inline(always)]
    fn deserialize(mut de: Deserializer) -> Result<Self, DeserializeError> {
        let byte = de.read_byte()?;
        Ok(T::from(byte != 0))
    }

    #[inline(always)]
    fn deserialize_in_place(&mut self, mut de: Deserializer) -> Result<(), DeserializeError> {
        let byte = de.read_byte()?;
        *self = From::from(byte != 0);
        Ok(())
    }
}