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
use crate::*;

macro_rules! impl_data_type_for_typle {
    [$(($($name: ident : $idx: tt),*)),*]  => (
        $(
            impl<$($name,)*> Encoder for ($($name,)*)
            where
                $($name: Encoder,)*
            {
                const SIZE: usize = 0 $(+$name::SIZE)*;

                #[inline]
                fn size_hint(&self) -> usize { 0 $(+ self.$idx.size_hint())* }

                #[inline]
                fn encoder(&self, _c: &mut impl Array<u8>) {
                    $(self.$idx.encoder(_c);)*
                }
            }

            impl<'de, $($name,)*> Decoder<'de> for ($($name,)*)
            where
                $($name: Decoder<'de>,)*
            {
                #[inline]
                fn decoder(_c: &mut Cursor<&'de [u8]>) -> Result<Self, &'static str> {
                    Ok(($($name::decoder(_c)?),*))
                }
            }
        )*
    );
}
impl_data_type_for_typle!(
    (),
    (T:0, T2:1),
    (T:0, T2:1, T3:2),
    (T:0, T2:1, T3:2, T4:3),
    (T:0, T2:1, T3:2, T4:3, T5:4),
    (T:0, T2:1, T3:2, T4:3, T5:4, T6:5),
    (T:0, T2:1, T3:2, T4:3, T5:4, T6:5, T7:6)
);

impl<T: Encoder, const N: usize> Encoder for [T; N] {
    const SIZE: usize = N * T::SIZE;

    #[inline]
    fn size_hint(&self) -> usize {
        self.iter().map(T::size_hint).sum()
    }

    #[inline]
    fn encoder(&self, c: &mut impl Array<u8>) {
        for item in self {
            item.encoder(c);
        }
    }
}
impl<'de, T: Decoder<'de>, const N: usize> Decoder<'de> for [T; N] {
    #[inline]
    fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, &'static str> {
        #[cfg(feature = "nightly")]
        return [(); N].try_map(|_| T::decoder(c));

        #[cfg(not(feature = "nightly"))]
        return (0..N)
            .map(|_| T::decoder(c))
            .collect::<Result<Vec<_>, _>>()
            .map(|v| unsafe { v.try_into().unwrap_unchecked() });
    }
}

impl<const N: usize> Encoder for &[u8; N] {
    const SIZE: usize = N;

    #[inline]
    fn encoder(&self, c: &mut impl Array<u8>) {
        c.extend_from_slice(self);
    }
}

impl<'de, const N: usize> Decoder<'de> for &'de [u8; N] {
    #[inline]
    fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, &'static str> {
        c.read_slice(N)
            .ok_or("Insufficient bytes")
            .map(|bytes| unsafe { bytes.try_into().unwrap_unchecked() })
    }
}