sbor/
decode.rs

1use crate::decoder::*;
2use crate::value_kind::*;
3
4/// A data structure that can be decoded from a byte array using SBOR.
5pub trait Decode<X: CustomValueKind, D: Decoder<X>>: Sized {
6    /// Decodes the type from the decoder, which should match a preloaded value kind.
7    ///
8    /// You may want to call `decoder.decode_deeper_body_with_value_kind` instead of this method. See
9    /// the below section for details.
10    ///
11    /// ## Direct calls and SBOR Depth
12    ///
13    /// In order to avoid SBOR depth differentials and disagreement about whether a payload
14    /// is valid, typed codec implementations should ensure that the SBOR depth as measured
15    /// during the encoding/decoding process agrees with the SBOR [`Value`][crate::Value] codec.
16    ///
17    /// Each layer of the SBOR `Value` counts as one depth.
18    ///
19    /// If the decoder you're writing is embedding a child type (and is represented as such
20    /// in the SBOR `Value` type), then you should call `decoder.decode_body_with_value_kind` to increment
21    /// the SBOR depth tracker.
22    ///
23    /// You should only call `T::decode_body_with_value_kind` directly when the decoding of that type
24    /// into an SBOR `Value` doesn't increase the SBOR depth in the decoder, that is:
25    /// * When the wrapping type is invisible to the SBOR `Value`, ie:
26    ///   * Smart pointers
27    ///   * Transparent wrappers
28    /// * Where the use of the inner type is invisible to SBOR `Value`, ie:
29    ///   * Where the use of `T::decode_body_with_value_kind` is coincidental / code re-use
30    fn decode_body_with_value_kind(
31        decoder: &mut D,
32        value_kind: ValueKind<X>,
33    ) -> Result<Self, DecodeError>;
34}