Trait kitsune_p2p_types::codec::Codec

source ·
pub trait Codec: Clone + Sized {
    // Required methods
    fn variant_type(&self) -> &'static str;
    fn encode<W>(&self, w: &mut W) -> Result<(), Error>
       where W: Write;
    fn decode<R>(r: &mut R) -> Result<Self, Error>
       where R: Read;

    // Provided methods
    fn encode_vec(&self) -> Result<Vec<u8>, Error> { ... }
    fn decode_ref(r: &[u8]) -> Result<(u64, Self), Error> { ... }
}
Expand description

Apply to a data item to indicate it can be encoded / decoded.

Required Methods§

source

fn variant_type(&self) -> &'static str

Variant identifier (for debugging or as a cheap discriminant).

source

fn encode<W>(&self, w: &mut W) -> Result<(), Error>
where W: Write,

Encode this item to given writer. You may wish to first wrap your writer in a BufWriter.

source

fn decode<R>(r: &mut R) -> Result<Self, Error>
where R: Read,

Decode a reader into this item. You may wish to first wrap your reader in a BufReader.

Provided Methods§

source

fn encode_vec(&self) -> Result<Vec<u8>, Error>

Encode this item to an owned vector of bytes. Uses encode() internally.

source

fn decode_ref(r: &[u8]) -> Result<(u64, Self), Error>

Decode a range of bytes into this item. Will also return the byte count read. Uses decode() internally.

Object Safety§

This trait is not object safe.

Implementors§