Codec

Trait Codec 

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

    const CODE: u64;

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

    // 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>(reader: R) -> Result<T, Self::Error>
where R: BufRead,

Decode a reader into the desired type.

Source

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

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.

Implementations on Foreign Types§

Source§

impl<T> Codec<T> for DagCborCodec
where T: for<'a> Deserialize<'a> + Serialize,

Source§

const CODE: u64 = 113u64

Source§

type Error = CodecError

Source§

fn decode<R>(reader: R) -> Result<T, <DagCborCodec as Codec<T>>::Error>
where R: BufRead,

Source§

fn encode<W>( writer: W, data: &T, ) -> Result<(), <DagCborCodec as Codec<T>>::Error>
where W: Write,

Implementors§