dharitri_sc_codec/impl_for_types/
impl_num_unsigned.rs

1use crate::{
2    dep_encode_num_mimic, DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode,
3    NestedDecodeInput, NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode,
4    TopEncodeOutput,
5};
6
7// No reversing needed for u8, because it is a single byte.
8impl NestedEncode for u8 {
9    #[inline]
10    fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, _h: H) -> Result<(), H::HandledErr>
11    where
12        O: NestedEncodeOutput,
13        H: EncodeErrorHandler,
14    {
15        dest.push_byte(*self);
16        Ok(())
17    }
18
19    fn if_u8<Output, If, Else, R>(input: Output, if_branch: If, _else_branch: Else) -> R
20    where
21        If: FnOnce(Output) -> R,
22        Else: FnOnce(Output) -> R,
23    {
24        if_branch(input)
25    }
26}
27
28dep_encode_num_mimic! {usize, u32}
29
30// The main unsigned types need to be reversed before serializing.
31macro_rules! dep_encode_num_unsigned {
32    ($num_type:ty, $size_in_bits:expr) => {
33        impl NestedEncode for $num_type {
34            #[inline]
35            fn dep_encode_or_handle_err<O, H>(
36                &self,
37                dest: &mut O,
38                _h: H,
39            ) -> Result<(), H::HandledErr>
40            where
41                O: NestedEncodeOutput,
42                H: EncodeErrorHandler,
43            {
44                dest.write(&self.to_be_bytes()[..]);
45                Ok(())
46            }
47        }
48    };
49}
50
51dep_encode_num_unsigned! {u64, 64}
52dep_encode_num_unsigned! {u32, 32}
53dep_encode_num_unsigned! {u16, 16}
54
55macro_rules! top_encode_num_unsigned {
56    ($num_type:ty, $size_in_bits:expr) => {
57        impl TopEncode for $num_type {
58            #[inline]
59            fn top_encode_or_handle_err<O, H>(&self, output: O, _h: H) -> Result<(), H::HandledErr>
60            where
61                O: TopEncodeOutput,
62                H: EncodeErrorHandler,
63            {
64                output.set_u64(*self as u64);
65                Ok(())
66            }
67        }
68    };
69}
70
71top_encode_num_unsigned! {u64, 64}
72top_encode_num_unsigned! {u32, 32}
73top_encode_num_unsigned! {usize, 32}
74top_encode_num_unsigned! {u16, 16}
75top_encode_num_unsigned! {u8, 8}
76
77impl NestedDecode for u8 {
78    fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
79    where
80        I: NestedDecodeInput,
81        H: DecodeErrorHandler,
82    {
83        input.read_byte(h)
84    }
85
86    fn if_u8<Input, If, Else, R>(input: Input, if_branch: If, _else_branch: Else) -> R
87    where
88        If: FnOnce(Input) -> R,
89        Else: FnOnce(Input) -> R,
90    {
91        if_branch(input)
92    }
93}
94
95macro_rules! dep_decode_num_unsigned {
96    ($ty:ty, $num_bytes:expr) => {
97        impl NestedDecode for $ty {
98            fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
99            where
100                I: NestedDecodeInput,
101                H: DecodeErrorHandler,
102            {
103                let mut bytes = [0u8; $num_bytes];
104                input.read_into(&mut bytes[..], h)?;
105                let num = <$ty>::from_be_bytes(bytes);
106                Ok(num)
107            }
108        }
109    };
110}
111
112dep_decode_num_unsigned!(u16, 2);
113dep_decode_num_unsigned!(u32, 4);
114dep_decode_num_unsigned!(u64, 8);
115
116impl NestedDecode for usize {
117    fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
118    where
119        I: NestedDecodeInput,
120        H: DecodeErrorHandler,
121    {
122        u32::dep_decode_or_handle_err(input, h).map(|num| num as usize)
123    }
124}
125
126macro_rules! top_decode_num_unsigned {
127    ($ty:ty, $bounds_ty:ty) => {
128        impl TopDecode for $ty {
129            fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
130            where
131                I: TopDecodeInput,
132                H: DecodeErrorHandler,
133            {
134                let arg_u64 = input.into_u64(h)?;
135                let max = <$bounds_ty>::MAX as u64;
136                if arg_u64 > max {
137                    Err(h.handle_error(DecodeError::INPUT_TOO_LONG))
138                } else {
139                    Ok(arg_u64 as $ty)
140                }
141            }
142        }
143    };
144}
145
146top_decode_num_unsigned!(u8, u8);
147top_decode_num_unsigned!(u16, u16);
148top_decode_num_unsigned!(u32, u32);
149top_decode_num_unsigned!(usize, u32); // even if usize can be 64 bits on some platforms, we always deserialize as max 32 bits
150top_decode_num_unsigned!(u64, u64);
151
152#[cfg(test)]
153pub mod tests {
154    use crate::test_util::{check_dep_encode_decode, check_top_encode_decode};
155
156    #[test]
157    fn test_top() {
158        // unsigned zero
159        check_top_encode_decode(0u8, &[]);
160        check_top_encode_decode(0u16, &[]);
161        check_top_encode_decode(0u32, &[]);
162        check_top_encode_decode(0u64, &[]);
163        check_top_encode_decode(0usize, &[]);
164        // unsigned positive
165        check_top_encode_decode(5u8, &[5]);
166        check_top_encode_decode(5u16, &[5]);
167        check_top_encode_decode(5u32, &[5]);
168        check_top_encode_decode(5u64, &[5]);
169        check_top_encode_decode(5usize, &[5]);
170    }
171
172    #[test]
173    fn test_dep() {
174        // unsigned zero
175        check_dep_encode_decode(0u8, &[0]);
176        check_dep_encode_decode(0u16, &[0, 0]);
177        check_dep_encode_decode(0u32, &[0, 0, 0, 0]);
178        check_dep_encode_decode(0usize, &[0, 0, 0, 0]);
179        check_dep_encode_decode(0u64, &[0, 0, 0, 0, 0, 0, 0, 0]);
180        // unsigned positive
181        check_dep_encode_decode(5u8, &[5]);
182        check_dep_encode_decode(5u16, &[0, 5]);
183        check_dep_encode_decode(5u32, &[0, 0, 0, 5]);
184        check_dep_encode_decode(5usize, &[0, 0, 0, 5]);
185        check_dep_encode_decode(5u64, &[0, 0, 0, 0, 0, 0, 0, 5]);
186    }
187}