mls_rs_codec/
array.rs

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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use crate::{MlsDecode, MlsEncode, MlsSize};
use alloc::vec::Vec;

impl<const N: usize> MlsSize for [u8; N] {
    #[inline(always)]
    fn mls_encoded_len(&self) -> usize {
        N
    }
}

impl<const N: usize> MlsEncode for [u8; N] {
    #[inline(always)]
    fn mls_encode(&self, writer: &mut Vec<u8>) -> Result<(), crate::Error> {
        writer.extend_from_slice(self);
        Ok(())
    }
}

impl<const N: usize> MlsDecode for [u8; N] {
    fn mls_decode(reader: &mut &[u8]) -> Result<Self, crate::Error> {
        let array = reader
            .get(..N)
            .and_then(|head| head.try_into().ok())
            .ok_or(crate::Error::UnexpectedEOF)?;

        *reader = &reader[N..];
        Ok(array)
    }
}

#[cfg(test)]
mod tests {
    #[cfg(target_arch = "wasm32")]
    use wasm_bindgen_test::wasm_bindgen_test as test;

    use alloc::vec;

    use crate::{Error, MlsEncode};
    use assert_matches::assert_matches;

    #[test]
    fn serialize_works() {
        let arr = [0u8, 1u8, 2u8];
        assert_eq!(arr.mls_encode_to_vec().unwrap(), vec![0u8, 1u8, 2u8]);
    }

    #[test]
    fn serialize_round_trip() {
        let arr = [0u8, 1u8, 2u8];
        let serialized = arr.mls_encode_to_vec().unwrap();
        let restored: [u8; 3] = crate::MlsDecode::mls_decode(&mut &*serialized).unwrap();
        assert_eq!(arr, restored);
    }

    #[test]
    fn end_of_file_error() {
        let arr = [0u8, 1u8, 2u8];
        let serialized = arr.mls_encode_to_vec().unwrap();
        let res: Result<[u8; 5], Error> = crate::MlsDecode::mls_decode(&mut &*serialized);

        assert_matches!(res, Err(Error::UnexpectedEOF))
    }
}