elrond_codec/single/
top_de_input.rs

1use crate::{
2    num_conv::universal_decode_number, transmute::vec_into_boxed_slice, DecodeError,
3    DecodeErrorHandler, NestedDecodeInput, OwnedBytesNestedDecodeInput, TryStaticCast,
4};
5use alloc::{boxed::Box, vec::Vec};
6
7/// Trait that abstracts away an underlying API for a top-level object deserializer.
8/// The underlying API can provide pre-parsed i64/u64 or pre-bundled boxed slices.
9pub trait TopDecodeInput: Sized {
10    type NestedBuffer: NestedDecodeInput;
11
12    /// Length of the underlying data, in bytes.
13    fn byte_len(&self) -> usize;
14
15    /// Provides the underlying data as an owned byte slice box.
16    /// Consumes the input object in the process.
17    fn into_boxed_slice_u8(self) -> Box<[u8]>;
18
19    /// Puts the underlying data into a fixed size byte buffer
20    /// and returns the populated data slice from this buffer.
21    ///
22    /// Will return an error if the data exceeds the provided buffer.
23    fn into_max_size_buffer<H, const MAX_LEN: usize>(
24        self,
25        buffer: &mut [u8; MAX_LEN],
26        h: H,
27    ) -> Result<&[u8], H::HandledErr>
28    where
29        H: DecodeErrorHandler;
30
31    /// Retrieves the underlying data as a pre-parsed u64.
32    /// Expected to panic if the conversion is not possible.
33    ///
34    /// Consumes the input object in the process.
35    fn into_u64<H>(self, h: H) -> Result<u64, H::HandledErr>
36    where
37        H: DecodeErrorHandler,
38    {
39        let mut buffer = [0u8; 8];
40        let slice = self.into_max_size_buffer(&mut buffer, h)?;
41        Ok(universal_decode_number(slice, false))
42    }
43
44    /// Retrieves the underlying data as a pre-parsed i64.
45    /// Expected to panic if the conversion is not possible.
46    ///
47    /// Consumes the input object in the process.
48    fn into_i64<H>(self, h: H) -> Result<i64, H::HandledErr>
49    where
50        H: DecodeErrorHandler,
51    {
52        let mut buffer = [0u8; 8];
53        let slice = self.into_max_size_buffer(&mut buffer, h)?;
54        Ok(universal_decode_number(slice, true) as i64)
55    }
56
57    #[inline]
58    fn supports_specialized_type<T: TryStaticCast>() -> bool {
59        false
60    }
61
62    fn into_specialized<T, H>(self, h: H) -> Result<T, H::HandledErr>
63    where
64        T: TryStaticCast,
65        H: DecodeErrorHandler,
66    {
67        Err(h.handle_error(DecodeError::UNSUPPORTED_OPERATION))
68    }
69
70    fn into_nested_buffer(self) -> Self::NestedBuffer;
71}
72
73impl TopDecodeInput for Box<[u8]> {
74    type NestedBuffer = OwnedBytesNestedDecodeInput;
75
76    fn byte_len(&self) -> usize {
77        self.len()
78    }
79
80    fn into_boxed_slice_u8(self) -> Box<[u8]> {
81        self
82    }
83
84    fn into_max_size_buffer<H, const MAX_LEN: usize>(
85        self,
86        buffer: &mut [u8; MAX_LEN],
87        h: H,
88    ) -> Result<&[u8], H::HandledErr>
89    where
90        H: DecodeErrorHandler,
91    {
92        (&*self).into_max_size_buffer(buffer, h)
93    }
94
95    fn into_nested_buffer(self) -> Self::NestedBuffer {
96        OwnedBytesNestedDecodeInput::new(self)
97    }
98}
99
100impl TopDecodeInput for Vec<u8> {
101    type NestedBuffer = OwnedBytesNestedDecodeInput;
102
103    fn byte_len(&self) -> usize {
104        self.len()
105    }
106
107    fn into_boxed_slice_u8(self) -> Box<[u8]> {
108        vec_into_boxed_slice(self)
109    }
110
111    fn into_max_size_buffer<H, const MAX_LEN: usize>(
112        self,
113        buffer: &mut [u8; MAX_LEN],
114        h: H,
115    ) -> Result<&[u8], H::HandledErr>
116    where
117        H: DecodeErrorHandler,
118    {
119        self.as_slice().into_max_size_buffer(buffer, h)
120    }
121
122    fn into_nested_buffer(self) -> Self::NestedBuffer {
123        OwnedBytesNestedDecodeInput::new(self.into_boxed_slice())
124    }
125}
126
127impl<'a> TopDecodeInput for &'a [u8] {
128    type NestedBuffer = &'a [u8];
129
130    fn byte_len(&self) -> usize {
131        self.len()
132    }
133
134    fn into_boxed_slice_u8(self) -> Box<[u8]> {
135        Box::from(self)
136    }
137
138    fn into_max_size_buffer<H, const MAX_LEN: usize>(
139        self,
140        buffer: &mut [u8; MAX_LEN],
141        h: H,
142    ) -> Result<&[u8], H::HandledErr>
143    where
144        H: DecodeErrorHandler,
145    {
146        let l = self.len();
147        if l > MAX_LEN {
148            return Err(h.handle_error(DecodeError::INPUT_TOO_LONG));
149        }
150        buffer[..l].copy_from_slice(self);
151        Ok(&buffer[..l])
152    }
153
154    #[inline]
155    fn into_u64<H>(self, _h: H) -> Result<u64, H::HandledErr>
156    where
157        H: DecodeErrorHandler,
158    {
159        Ok(universal_decode_number(self, false))
160    }
161
162    #[inline]
163    fn into_i64<H>(self, _h: H) -> Result<i64, H::HandledErr>
164    where
165        H: DecodeErrorHandler,
166    {
167        Ok(universal_decode_number(self, true) as i64)
168    }
169
170    fn into_nested_buffer(self) -> Self::NestedBuffer {
171        self
172    }
173}