pub trait Encoder<X>: Sizedwhere
X: CustomValueKind,{
// Required methods
fn encode_deeper_body<T>(&mut self, value: &T) -> Result<(), EncodeError>
where T: Encode<X, Self> + ?Sized;
fn write_byte(&mut self, n: u8) -> Result<(), EncodeError>;
fn write_slice(&mut self, slice: &[u8]) -> Result<(), EncodeError>;
// Provided methods
fn encode_payload<T>(
self,
value: &T,
payload_prefix: u8
) -> Result<(), EncodeError>
where T: Encode<X, Self> + ?Sized { ... }
fn encode<T>(&mut self, value: &T) -> Result<(), EncodeError>
where T: Encode<X, Self> + ?Sized { ... }
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§
sourcefn encode_deeper_body<T>(&mut self, value: &T) -> Result<(), EncodeError>
fn encode_deeper_body<T>(&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
- Where the use of
fn write_byte(&mut self, n: u8) -> Result<(), EncodeError>
fn write_slice(&mut self, slice: &[u8]) -> Result<(), EncodeError>
Provided Methods§
sourcefn encode_payload<T>(
self,
value: &T,
payload_prefix: u8
) -> Result<(), EncodeError>
fn encode_payload<T>( 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
sourcefn encode<T>(&mut self, value: &T) -> Result<(), EncodeError>
fn encode<T>(&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.