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
use crate::{
    vec_into_boxed_slice, DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedEncode,
    NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
};
use alloc::{boxed::Box, vec::Vec};

/// Adds the concantenated encoded contents of a slice to an output buffer,
/// without serializing the slice length.
/// Byte slice is treated separately, via direct transmute.
pub fn dep_encode_slice_contents<T, O, H>(
    slice: &[T],
    dest: &mut O,
    h: H,
) -> Result<(), H::HandledErr>
where
    T: NestedEncode,
    O: NestedEncodeOutput,
    H: EncodeErrorHandler,
{
    T::if_u8(
        dest,
        |dest| {
            // cast &[T] to &[u8]
            let slice: &[u8] =
                unsafe { core::slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len()) };
            dest.write(slice);
            Ok(())
        },
        |dest| {
            for x in slice {
                x.dep_encode_or_handle_err(dest, h)?;
            }
            Ok(())
        },
    )
}

impl<T: NestedEncode> TopEncode for &[T] {
    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
    where
        O: TopEncodeOutput,
        H: EncodeErrorHandler,
    {
        T::if_u8(
            output,
            |output| {
                // transmute to &[u8]
                // save directly, without passing through the buffer
                let slice: &[u8] =
                    unsafe { core::slice::from_raw_parts(self.as_ptr() as *const u8, self.len()) };
                output.set_slice_u8(slice);
                Ok(())
            },
            |output| {
                // only using `dep_encode_slice_contents` for non-u8,
                // because it always appends to the buffer,
                // which is not necessary above
                let mut buffer = output.start_nested_encode();
                dep_encode_slice_contents(self, &mut buffer, h)?;
                output.finalize_nested_encode(buffer);
                Ok(())
            },
        )
    }
}

impl<T: NestedEncode> TopEncode for Box<[T]> {
    #[inline]
    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
    where
        O: TopEncodeOutput,
        H: EncodeErrorHandler,
    {
        self.as_ref().top_encode_or_handle_err(output, h)
    }
}

// Allowed to implement this because [T] cannot implement NestedDecode, being ?Sized.
impl<T: NestedDecode> TopDecode for Box<[T]> {
    fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
    where
        I: TopDecodeInput,
        H: DecodeErrorHandler,
    {
        T::if_u8(
            input,
            |input| {
                let bytes = input.into_boxed_slice_u8();
                let cast_bytes: Box<[T]> = unsafe { core::mem::transmute(bytes) };
                Ok(cast_bytes)
            },
            |input| {
                let vec = Vec::<T>::top_decode_or_handle_err(input, h)?;
                Ok(vec_into_boxed_slice(vec))
            },
        )
    }
}

impl<T: NestedEncode> NestedEncode for &[T] {
    fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr>
    where
        O: NestedEncodeOutput,
        H: EncodeErrorHandler,
    {
        // push size
        self.len().dep_encode_or_handle_err(dest, h)?;
        // actual data
        dep_encode_slice_contents(self, dest, h)
    }
}

impl<T: NestedEncode> NestedEncode for Box<[T]> {
    fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr>
    where
        O: NestedEncodeOutput,
        H: EncodeErrorHandler,
    {
        self.as_ref().dep_encode_or_handle_err(dest, h)
    }
}

// TODO: NestedDecode for Box<[T]> missing