multiversx_sc_codec/single/
top_en.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::{
    codec_err::EncodeError, DefaultErrorHandler, EncodeErrorHandler, NestedEncode,
    PanicErrorHandler, TopEncodeOutput,
};
use alloc::vec::Vec;
use unwrap_infallible::UnwrapInfallible;

pub trait TopEncode: Sized {
    /// Attempt to serialize the value to ouput.
    fn top_encode<O>(&self, output: O) -> Result<(), EncodeError>
    where
        O: TopEncodeOutput,
    {
        self.top_encode_or_handle_err(output, DefaultErrorHandler)
    }

    /// Version of `top_encode` that can handle errors as soon as they occur.
    /// For instance in can exit immediately and make sure that if it returns, it is a success.
    /// By not deferring error handling, this can lead to somewhat smaller bytecode.
    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
    where
        O: TopEncodeOutput,
        H: EncodeErrorHandler,
    {
        match self.top_encode(output) {
            Ok(()) => Ok(()),
            Err(e) => Err(h.handle_error(e)),
        }
    }
}

pub fn top_encode_from_nested<T, O, H>(obj: &T, output: O, h: H) -> Result<(), H::HandledErr>
where
    O: TopEncodeOutput,
    T: NestedEncode,
    H: EncodeErrorHandler,
{
    let mut nested_buffer = output.start_nested_encode();
    obj.dep_encode_or_handle_err(&mut nested_buffer, h)?;
    output.finalize_nested_encode(nested_buffer);
    Ok(())
}

pub fn top_encode_to_vec_u8<T: TopEncode>(obj: &T) -> Result<Vec<u8>, EncodeError> {
    let mut bytes = Vec::<u8>::new();
    obj.top_encode(&mut bytes)?;
    Ok(bytes)
}

pub fn top_encode_to_vec_u8_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
    let mut bytes = Vec::<u8>::new();
    obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler)
        .unwrap_infallible();
    bytes
}