testnumbat_codec/
nested_ser_output.rs

1use alloc::vec::Vec;
2
3use crate::{EncodeError, TryStaticCast};
4
5/// Trait that allows appending bytes.
6/// Used especially by the NestedEncode trait to output data.
7///
8/// In principle it can be anything, but in practice
9/// we only keep 1 implementation, which is Vec<u8>.
10/// This is to avoid code duplication by monomorphization.
11pub trait NestedEncodeOutput {
12    /// Write to the output.
13    fn write(&mut self, bytes: &[u8]);
14
15    /// Write a single byte to the output.
16    fn push_byte(&mut self, byte: u8) {
17        self.write(&[byte]);
18    }
19
20    #[inline]
21    fn push_specialized<T, C, F>(
22        &mut self,
23        _context: C,
24        _value: &T,
25        else_serialization: F,
26    ) -> Result<(), EncodeError>
27    where
28        T: TryStaticCast,
29        C: TryStaticCast,
30        F: FnOnce(&mut Self) -> Result<(), EncodeError>,
31    {
32        else_serialization(self)
33    }
34}
35
36impl NestedEncodeOutput for Vec<u8> {
37    fn write(&mut self, bytes: &[u8]) {
38        self.extend_from_slice(bytes)
39    }
40}