numbat_codec/nested_de.rs
1use crate::{codec_err::DecodeError, nested_de_input::NestedDecodeInput, TypeInfo};
2
3/// Trait that allows zero-copy read of value-references from slices in LE format.
4pub trait NestedDecode: Sized {
5 // !INTERNAL USE ONLY!
6 // This const helps numbat-wasm to optimize the encoding/decoding by doing fake specialization.
7 #[doc(hidden)]
8 const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
9
10 /// Attempt to deserialise the value from input,
11 /// using the format of an object nested inside another structure.
12 /// In case of success returns the deserialized value and the number of bytes consumed during the operation.
13 fn dep_decode<I: NestedDecodeInput>(input: &mut I) -> Result<Self, DecodeError>;
14
15 /// Version of `top_decode` that exits quickly in case of error.
16 /// Its purpose is to create smaller implementations
17 /// in cases where the application is supposed to exit directly on decode error.
18 fn dep_decode_or_exit<I: NestedDecodeInput, ExitCtx: Clone>(
19 input: &mut I,
20 c: ExitCtx,
21 exit: fn(ExitCtx, DecodeError) -> !,
22 ) -> Self {
23 match Self::dep_decode(input) {
24 Ok(v) => v,
25 Err(e) => exit(c, e),
26 }
27 }
28}