drt_sc_codec/single/
top_en_output.rs

1use crate::{
2    num_conv::{top_encode_number, top_encode_number_buffer},
3    EncodeError, EncodeErrorHandler, NestedEncodeOutput, TryStaticCast,
4};
5use alloc::vec::Vec;
6
7/// Specifies objects that can receive the result of a TopEncode computation.
8
9/// in principle from NestedEncode performed on nested items.
10///
11/// All methods consume the object, so they can only be called once.
12///
13/// The trait is used in 3 scenarios:
14/// - SC results
15/// - `#[storage_set(...)]`
16/// - Serialize async call.
17pub trait TopEncodeOutput: Sized {
18    /// Type of `NestedEncodeOutput` that can be spawned to gather serializations of children.
19    type NestedBuffer: NestedEncodeOutput;
20
21    fn set_slice_u8(self, bytes: &[u8]);
22
23    fn set_u64(self, value: u64) {
24        let mut buffer = top_encode_number_buffer();
25        let slice = top_encode_number(value, false, &mut buffer);
26        self.set_slice_u8(slice);
27    }
28
29    fn set_i64(self, value: i64) {
30        let mut buffer = top_encode_number_buffer();
31        let slice = top_encode_number(value as u64, true, &mut buffer);
32        self.set_slice_u8(slice);
33    }
34
35    /// The unit type `()` is serializable, but some TopEncodeOutput implementations might want to treat it differently.
36    /// For instance, SC function result units do not cause `finish` to be called, no empty result produced.
37    #[doc(hidden)]
38    #[inline]
39    fn set_unit(self) {
40        self.set_slice_u8(&[]);
41    }
42
43    #[inline]
44    fn supports_specialized_type<T: TryStaticCast>() -> bool {
45        false
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, H>(self, _value: &T, h: H) -> Result<(), H::HandledErr>
54    where
55        T: TryStaticCast,
56        H: EncodeErrorHandler,
57    {
58        Err(h.handle_error(EncodeError::UNSUPPORTED_OPERATION))
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}