Trait vector_buffers::encoding::FixedEncodable

source ·
pub trait FixedEncodable {
    type EncodeError: Error + Send + Sync + 'static;
    type DecodeError: Error + Send + Sync + 'static;

    // Required methods
    fn encode<B: BufMut>(self, buffer: &mut B) -> Result<(), Self::EncodeError>;
    fn decode<B: Buf + Clone>(buffer: B) -> Result<Self, Self::DecodeError>
       where Self: Sized;

    // Provided method
    fn encoded_size(&self) -> Option<usize> { ... }
}
Expand description

An object that can encode and decode itself to and from a buffer, with a fixed representation.

This trait is a companion trait to Encodable that provides a blanket implementation of Encodable that does not use or care about encoding metadata. It fulfills the necessary methods to work in Encodable contexts without requiring any of the boilerplate.

§Warning

You should not typically use this trait unless you’re trying to implement Encodable for testing purposes where you won’t be dealing with a need for versioning payloads, etc.

For any types that will potentially be encoded in real use cases, Encodable should be preferred as it requires an upfront decision to be made about metadata and how it’s dealt with.

Required Associated Types§

source

type EncodeError: Error + Send + Sync + 'static

source

type DecodeError: Error + Send + Sync + 'static

Required Methods§

source

fn encode<B: BufMut>(self, buffer: &mut B) -> Result<(), Self::EncodeError>

Attempts to encode this value into the given buffer.

§Errors

If there is an error while attempting to encode this value, an error variant will be returned describing the error.

Practically speaking, based on the API, encoding errors should generally only occur if there is insufficient space in the buffer to fully encode this value. However, this is not guaranteed.

source

fn decode<B: Buf + Clone>(buffer: B) -> Result<Self, Self::DecodeError>
where Self: Sized,

Attempts to decode an instance of this type from the given buffer.

§Errors

If there is an error while attempting to decode a value from the given buffer, an error variant will be returned describing the error.

Provided Methods§

source

fn encoded_size(&self) -> Option<usize>

Gets the encoded size, in bytes, of this value, if available.

Not all types can know ahead of time how many bytes they will occupy when encoded, hence the fallibility of this method.

Object Safety§

This trait is not object safe.

Implementors§