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§
Sourcetype EncodingError: Error
type EncodingError: Error
Encoding error type.
Sourcetype DecodingError: Error
type DecodingError: Error
Decoding error type.
Required Methods§
Sourcefn multicodec_code(&self) -> u64
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.
Try to create a codec from a series of tags.
Sourcefn encode_payload<W: Write>(
&self,
payload: &T,
buffer: &mut W,
) -> Result<(), Self::EncodingError>
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.
Sourcefn decode_payload<R: BufRead>(
&self,
reader: &mut R,
) -> Result<T, Self::DecodingError>
fn decode_payload<R: BufRead>( &self, reader: &mut R, ) -> Result<T, Self::DecodingError>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".