Skip to main content

Codec

Trait Codec 

Source
pub trait Codec<T>: Sized {
    type EncodingError: Error;
    type DecodingError: Error;

    // Required methods
    fn multicodec_code(&self) -> u64;
    fn try_from_tags(code: &[u64]) -> Option<Self>;
    fn encode_payload<W: Write>(
        &self,
        payload: &T,
        buffer: &mut W,
    ) -> Result<(), Self::EncodingError>;
    fn decode_payload<R: BufRead>(
        &self,
        reader: &mut R,
    ) -> Result<T, Self::DecodingError>;
}
Expand description

IPLD Codec trait.

This trait is a generalization of the libipld_core::codec::Codec trait. Specifically this allows an application to accept multiple codecs and distinguish with a runtime enum. This is important for Varsig, since it may need to encode to the configured codec for signature verification.

An implementation is provided for types that have ipld_core::codec::Codec.

Required Associated Types§

Source

type EncodingError: Error

Encoding error type.

Source

type DecodingError: Error

Decoding error type.

Required Methods§

Source

fn multicodec_code(&self) -> u64

Multicodec code.

This is not a const because an implementation may support more than one IPLD codec, so it is runtime dependent.

Source

fn try_from_tags(code: &[u64]) -> Option<Self>

Try to create a codec from a series of tags.

Source

fn encode_payload<W: Write>( &self, payload: &T, buffer: &mut W, ) -> Result<(), Self::EncodingError>

Encode the payload to the given buffer.

§Parameters
  • payload: The payload to encode.
  • buffer: The buffer to write the encoded payload to.
§Returns

Returns Ok(()) on success, or an error of type Self::EncodingError on failure.

§Errors

If the encoding fails, it returns an error of type Self::EncodingError.

Source

fn decode_payload<R: BufRead>( &self, reader: &mut R, ) -> Result<T, Self::DecodingError>

Decode the payload from the given reader.

§Parameters
  • reader: The reader to read the encoded payload from.
§Returns

Returns the decoded payload of type T on success, or an error of type Self::DecodingError on failure.

§Errors

If the decoding fails, it returns an error of type Self::DecodingError.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl<T: Serialize + for<'de> Deserialize<'de>> Codec<T> for DagCborCodec

Implementors§