multiversx_sc_codec/multi/
top_de_multi.rs

1use crate::{
2    DecodeError, DecodeErrorHandler, DefaultErrorHandler, TopDecode, TopDecodeMultiInput, TopEncode,
3};
4
5pub trait TopDecodeMulti: Sized {
6    /// Used to optimize single value loading of endpoint arguments.
7    const IS_SINGLE_VALUE: bool = false;
8
9    fn multi_decode<I>(input: &mut I) -> Result<Self, DecodeError>
10    where
11        I: TopDecodeMultiInput,
12    {
13        Self::multi_decode_or_handle_err(input, DefaultErrorHandler)
14    }
15
16    fn multi_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
17    where
18        I: TopDecodeMultiInput,
19        H: DecodeErrorHandler,
20    {
21        match Self::multi_decode(input) {
22            Ok(v) => Ok(v),
23            Err(e) => Err(h.handle_error(e)),
24        }
25    }
26}
27pub trait TopDecodeMultiLength {
28    const LEN: usize;
29    fn get_len() -> usize {
30        Self::LEN
31    }
32}
33
34/// All single top decode types also work as multi-value decode types.
35impl<T> TopDecodeMulti for T
36where
37    T: TopDecode,
38{
39    const IS_SINGLE_VALUE: bool = true;
40
41    fn multi_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
42    where
43        I: TopDecodeMultiInput,
44        H: DecodeErrorHandler,
45    {
46        input.next_value(h)
47    }
48}
49
50impl<T> TopDecodeMultiLength for T
51where
52    T: TopEncode + TopDecode,
53{
54    const LEN: usize = 1;
55}