multiversx_sc_codec/single/
nested_en_output.rs

1use alloc::vec::Vec;
2
3use crate::{EncodeError, EncodeErrorHandler, 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 supports_specialized_type<T: TryStaticCast>() -> bool {
22        false
23    }
24
25    #[inline]
26    fn push_specialized<T, C, H>(
27        &mut self,
28        _context: C,
29        _value: &T,
30        h: H,
31    ) -> Result<(), H::HandledErr>
32    where
33        T: TryStaticCast,
34        C: TryStaticCast,
35        H: EncodeErrorHandler,
36    {
37        Err(h.handle_error(EncodeError::UNSUPPORTED_OPERATION))
38    }
39}
40
41impl NestedEncodeOutput for Vec<u8> {
42    fn write(&mut self, bytes: &[u8]) {
43        self.extend_from_slice(bytes)
44    }
45}