Codec

Trait Codec 

Source
pub trait Codec<T> {
    type Error;
    type Compact;

    // Required methods
    fn encode(val: &T) -> Result<Self::Compact, Self::Error>;
    fn decode(val: &Self::Compact) -> Result<T, Self::Error>;
}
Expand description

A trait for converting values between a type T and a more compact or transport-friendly representation for a Backend. Examples include json and bytes.

This is useful when you need to serialize/deserialize, compress/expand, or otherwise encode/decode values in a custom format.

By default, a backend doesn’t care about the specific type implementing Codec but rather the Codec::Compact type. This means if it can accept bytes, you can use familiar crates such as bincode and rkyv

§Type Parameters

  • T: The type of value being encoded/decoded.

Required Associated Types§

Source

type Error

The error type returned if encoding or decoding fails.

Source

type Compact

The compact or encoded representation of T.

This could be a primitive type, a byte buffer, or any other representation that is more efficient to store or transmit.

Required Methods§

Source

fn encode(val: &T) -> Result<Self::Compact, Self::Error>

Encode a value of type T into its compact representation.

§Errors

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

Source

fn decode(val: &Self::Compact) -> Result<T, Self::Error>

Decode a compact representation back into a value of type T.

§Errors

Returns Self::Error if the compact representation cannot be decoded into a valid T.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> Codec<T> for IdentityCodec
where T: Clone,

Source§

impl<T> Codec<T> for NoopCodec

Source§

impl<T: Serialize + DeserializeOwned> Codec<T> for JsonCodec<Value>

Available on crate feature json only.
Source§

impl<T: Serialize + DeserializeOwned> Codec<T> for JsonCodec<String>

Available on crate feature json only.
Source§

impl<T: Serialize + DeserializeOwned> Codec<T> for JsonCodec<Vec<u8>>

Available on crate feature json only.