Trait sbor::encoder::Encoder

source ·
pub trait Encoder<X: CustomValueKind>: Sized {
    // Required methods
    fn encode_deeper_body<T: Encode<X, Self> + ?Sized>(
        &mut self,
        value: &T
    ) -> Result<(), EncodeError>;
    fn write_byte(&mut self, n: u8) -> Result<(), EncodeError>;
    fn write_slice(&mut self, slice: &[u8]) -> Result<(), EncodeError>;

    // Provided methods
    fn encode_payload<T: Encode<X, Self> + ?Sized>(
        self,
        value: &T,
        payload_prefix: u8
    ) -> Result<(), EncodeError> { ... }
    fn encode<T: Encode<X, Self> + ?Sized>(
        &mut self,
        value: &T
    ) -> Result<(), EncodeError> { ... }
    fn write_payload_prefix(
        &mut self,
        payload_prefix: u8
    ) -> Result<(), EncodeError> { ... }
    fn write_value_kind(&mut self, ty: ValueKind<X>) -> Result<(), EncodeError> { ... }
    fn write_discriminator(
        &mut self,
        discriminator: u8
    ) -> Result<(), EncodeError> { ... }
    fn write_size(&mut self, size: usize) -> Result<(), EncodeError> { ... }
}

Required Methods§

source

fn encode_deeper_body<T: Encode<X, Self> + ?Sized>( &mut self, value: &T ) -> Result<(), EncodeError>

Encodes the SBOR body of a child value as part of a larger payload.

In many cases, you may wish to directly call value.encode_body instead of this method. See the below section for details.

§Direct calls and SBOR Depth

In order to avoid SBOR depth differentials and disagreement about whether a payload is valid, typed codec implementations should ensure that the SBOR depth as measured during the encoding/decoding process agrees with the Value codec.

Each layer of the Value counts as one depth.

If the encoder you’re writing is embedding a child type (and is represented as such in the Value type), then you should call encoder.encode_body to increment the SBOR depth tracker.

You should call value.encode_body directly when the encoding of that type into an Value doesn’t increase the SBOR depth in the encoder, that is:

  • When the wrapping type is invisible to the Value, ie:
    • Smart pointers
    • Transparent wrappers
  • Where the use of the inner type is invisible to Value, ie:
    • Where the use of value.encode_body is coincidental / code re-use
source

fn write_byte(&mut self, n: u8) -> Result<(), EncodeError>

source

fn write_slice(&mut self, slice: &[u8]) -> Result<(), EncodeError>

Provided Methods§

source

fn encode_payload<T: Encode<X, Self> + ?Sized>( self, value: &T, payload_prefix: u8 ) -> Result<(), EncodeError>

Consumes the Encoder and encodes the value as a full payload

It starts by writing the payload prefix: It’s the intention that each version of SBOR or change to the custom codecs should be given its own prefix

source

fn encode<T: Encode<X, Self> + ?Sized>( &mut self, value: &T ) -> Result<(), EncodeError>

Encodes the value as part of a larger payload

This method encodes the SBOR value’s kind and then its body.

source

fn write_payload_prefix( &mut self, payload_prefix: u8 ) -> Result<(), EncodeError>

source

fn write_value_kind(&mut self, ty: ValueKind<X>) -> Result<(), EncodeError>

source

fn write_discriminator(&mut self, discriminator: u8) -> Result<(), EncodeError>

source

fn write_size(&mut self, size: usize) -> Result<(), EncodeError>

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'a, X: CustomValueKind> Encoder<X> for VecEncoder<'a, X>