Skip to main content

Codec

Trait Codec 

Source
pub trait Codec<T> {
    // Required methods
    fn encode(&self, value: &T) -> Result<Vec<u8>>;
    fn decode(&self, bytes: &[u8]) -> Result<T>;
}
Expand description

Encodes and decodes values of type T to and from byte buffers.

A codec is the bridge between in-memory values and the bytes carried by a Transport or persisted by a Store.

Required Methods§

Source

fn encode(&self, value: &T) -> Result<Vec<u8>>

Encodes a value into a byte buffer.

§Arguments
  • value - the value to serialize.
§Returns

A byte buffer containing the encoded representation of value.

§Errors

Returns Error::Codec if the value cannot be encoded.

Source

fn decode(&self, bytes: &[u8]) -> Result<T>

Decodes a value from a byte buffer.

§Arguments
  • bytes - the encoded representation to deserialize.
§Returns

The value decoded from bytes.

§Errors

Returns Error::Codec if bytes is not a valid encoding of T.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§