testnumbat_codec/
top_ser_output.rs

1use crate::{
2    num_conv::top_encode_number_to_output, EncodeError, NestedEncodeOutput, TryStaticCast,
3};
4use alloc::{boxed::Box, vec::Vec};
5
6/// Specifies objects that can receive the result of a TopEncode computation.
7
8/// in principle from NestedEncode performed on nested items.
9///
10/// All methods consume the object, so they can only be called once.
11///
12/// The trait is used in 3 scenarios:
13/// - SC results
14/// - `#[storage_set(...)]`
15/// - Serialize async call.
16pub trait TopEncodeOutput: Sized {
17    /// Type of `NestedEncodeOutput` that can be spawned to gather serializations of children.
18    type NestedBuffer: NestedEncodeOutput;
19
20    fn set_slice_u8(self, bytes: &[u8]);
21
22    #[inline]
23    #[allow(clippy::boxed_local)]
24    fn set_boxed_bytes(self, bytes: Box<[u8]>) {
25        self.set_slice_u8(&*bytes);
26    }
27
28    fn set_u64(self, value: u64) {
29        let mut buffer = Vec::<u8>::with_capacity(8);
30        top_encode_number_to_output(&mut buffer, value, false);
31        self.set_slice_u8(&buffer[..]);
32    }
33
34    fn set_i64(self, value: i64) {
35        let mut buffer = Vec::<u8>::with_capacity(8);
36        top_encode_number_to_output(&mut buffer, value as u64, true);
37        self.set_slice_u8(&buffer[..]);
38    }
39
40    /// The unit type `()` is serializable, but some TopEncodeOutput implementations might want to treat it differently.
41    /// For instance, SC function result units do not cause `finish` to be called, no empty result produced.
42    #[doc(hidden)]
43    #[inline]
44    fn set_unit(self) {
45        self.set_slice_u8(&[]);
46    }
47
48    /// Allows special handling of special types.
49    /// Also requires an alternative serialization, in case the special handling is not covered.
50    /// The alternative serialization, `else_serialization` is only called when necessary and
51    /// is normally compiled out via monomorphization.
52    #[inline]
53    fn set_specialized<T, F>(self, _value: &T, else_serialization: F) -> Result<(), EncodeError>
54    where
55        T: TryStaticCast,
56        F: FnOnce(Self) -> Result<(), EncodeError>,
57    {
58        else_serialization(self)
59    }
60
61    fn start_nested_encode(&self) -> Self::NestedBuffer;
62
63    fn finalize_nested_encode(self, nb: Self::NestedBuffer);
64}
65
66impl TopEncodeOutput for &mut Vec<u8> {
67    type NestedBuffer = Vec<u8>;
68
69    fn set_slice_u8(self, bytes: &[u8]) {
70        self.extend_from_slice(bytes);
71    }
72
73    fn start_nested_encode(&self) -> Self::NestedBuffer {
74        Vec::<u8>::new()
75    }
76
77    fn finalize_nested_encode(self, nb: Self::NestedBuffer) {
78        *self = nb;
79    }
80}