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
use std::mem::MaybeUninit;


impl<T, const N: usize> crate::ByteDecode for [T; N]
where
    T: crate::ByteDecode,
{
    fn decode<'da, 'db>(input: &'da [u8], cattr: Option<&'db crate::ContainerAttrModifiers>, fattr: Option<&'db crate::FieldAttrModifiers>) -> crate::JResult<&'da [u8], Self>
    where 
        Self: Sized
    {
        let mut array = MaybeUninit::<[T; N]>::uninit();
        let mut input = input;
        let mut value;

        let ptr = unsafe { &mut *array.as_mut_ptr() };

        for i in 0..N {
            (input, value) = T::decode(input, cattr, fattr)?;
            ptr[i] = value;
        }

        Ok((input, unsafe { array.assume_init() }))
    }
}


impl<'de, T, const N: usize> crate::BorrowByteDecode<'de> for [T; N]
where
    T: crate::BorrowByteDecode<'de>,
{
    fn decode<'da: 'de, 'db>(input: &'da [u8], cattr: Option<&'db crate::ContainerAttrModifiers>, fattr: Option<&'db crate::FieldAttrModifiers>) -> crate::JResult<&'da [u8], Self>
    where 
        Self: Sized
    {
        let mut array = MaybeUninit::<[T; N]>::uninit();
        let mut input = input;
        let mut value;

        let ptr = unsafe { &mut *array.as_mut_ptr() };

        for i in 0..N {
            (input, value) = T::decode(input, cattr, fattr)?;
            ptr[i] = value;
        }

        Ok((input, unsafe { array.assume_init() }))
    }
}


#[cfg(test)]
mod tests {
    use crate::decode::ByteDecode;

    #[test]
    fn test_decode_array() {
        let (input, value) = <[u8;2]>::decode(&[0x01, 0x02, 0x03], None, None).unwrap();
        assert_eq!(value, [0x01, 0x02]);
        assert_eq!(input, [0x03]);

        let (input, value) = <[u16;2]>::decode(&[0x00, 0x01, 0x00, 0x02, 0x03], None, None).unwrap();
        assert_eq!(value, [0x01, 0x02]);
        assert_eq!(input, [0x03]);
    }
}