Trait Codec

Source
pub trait Codec<T>: Links {
    type Error;

    const CODE: u64;

    // Required methods
    fn decode<R: BufRead>(reader: R) -> Result<T, Self::Error>;
    fn encode<W: Write>(writer: W, data: &T) -> Result<(), Self::Error>;

    // Provided methods
    fn decode_from_slice(bytes: &[u8]) -> Result<T, Self::Error> { ... }
    fn encode_to_vec(data: &T) -> Result<Vec<u8>, Self::Error> { ... }
}
Expand description

Each IPLD codec implementation should implement this Codec trait. This way codecs can be more easily exchanged or combined.

Required Associated Constants§

Source

const CODE: u64

The multicodec code of the IPLD codec.

Required Associated Types§

Source

type Error

The error that is returned if encoding or decoding fails.

Required Methods§

Source

fn decode<R: BufRead>(reader: R) -> Result<T, Self::Error>

Decode a reader into the desired type.

Source

fn encode<W: Write>(writer: W, data: &T) -> Result<(), Self::Error>

Encode a type into a writer.

Provided Methods§

Source

fn decode_from_slice(bytes: &[u8]) -> Result<T, Self::Error>

Decode a slice into the desired type.

Source

fn encode_to_vec(data: &T) -> Result<Vec<u8>, Self::Error>

Encode a type into bytes.

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§