Coder

Trait Coder 

Source
pub trait Coder:
    Clone
    + Send
    + Sync
    + 'static {
    // Required methods
    fn new() -> Self;
    fn format_name(&self) -> &'static str;
    fn encode_fmt1_to_buf<T: Serialize>(
        &self,
        obj: &T,
    ) -> Result<Vec<u8>, String>;
    fn decode_fmt1_from_bytes<'a, T: Deserialize<'a>>(
        &self,
        bytes: &'a [u8],
    ) -> Result<T, String>;
    fn encode_fmt1_list_to_buf<T: Serialize + IntoIterator>(
        &self,
        list: &T,
    ) -> Result<Vec<u8>, String>;
    fn fmt1_list_len(&self, bytes: &[u8]) -> Result<usize, String>;
    fn encode_fmt2_to_buf<T: Serialize>(
        &self,
        obj: &T,
    ) -> Result<Vec<u8>, String>;
    fn decode_fmt2_from_bytes<'a, T: Deserialize<'a>>(
        &self,
        bytes: &'a [u8],
    ) -> Result<T, String>;
    fn encode_fmt2_list_to_buf<T: Serialize + IntoIterator<Item = I>, I: Sized + Copy>(
        &self,
        list: &T,
    ) -> Result<Vec<u8>, String>;
    fn fmt2_list_len(&self, bytes: &[u8]) -> Result<usize, String>;
}
Expand description

Wraps an interface to an encode / decode format

NOTE: It’s unlikely you will want to implement this trait. Instead use one of the existing implementations: BincodeCoder and MsgPackCoder, or use DefaultCoder

NOTE: fmt1 and fmt2 allow the a coder to use two different formats. Originally this was done for BinCode’s varint and fixint formats, where fmt1 was the more compact, while fmt2 is the more regular and faster to parse.

Required Methods§

Source

fn new() -> Self

Create a new coder

Source

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

The name of the format, to check for compatibility

Source

fn encode_fmt1_to_buf<T: Serialize>(&self, obj: &T) -> Result<Vec<u8>, String>

Encodes an arbitrary structure to bytes using fmt1 encoding

Source

fn decode_fmt1_from_bytes<'a, T: Deserialize<'a>>( &self, bytes: &'a [u8], ) -> Result<T, String>

Decodes an arbitrary structure from bytes using a fmt1 encoding

Source

fn encode_fmt1_list_to_buf<T: Serialize + IntoIterator>( &self, list: &T, ) -> Result<Vec<u8>, String>

Encodes a list of items to a buffer using a fmt1 encoding

NOTE: it is OK to decode the returned buffer using decode_fmt1_from_bytes WARNING: This method requires the Serializer for T to encode the length as the first thing in the serialized output

Source

fn fmt1_list_len(&self, bytes: &[u8]) -> Result<usize, String>

Returns the number of elements in an encoded fmt1 list without decoding them

WARNING: This method assumes the bytes were encoded with encode_fmt1_list_to_buf

Source

fn encode_fmt2_to_buf<T: Serialize>(&self, obj: &T) -> Result<Vec<u8>, String>

Encodes an arbitrary structure to bytes using fmt2 encoding

Source

fn decode_fmt2_from_bytes<'a, T: Deserialize<'a>>( &self, bytes: &'a [u8], ) -> Result<T, String>

Decodes an arbitrary structure from bytes using fmt2 encoding

Source

fn encode_fmt2_list_to_buf<T: Serialize + IntoIterator<Item = I>, I: Sized + Copy>( &self, list: &T, ) -> Result<Vec<u8>, String>

Encodes a list of items to a buffer using fmt2 encoding

WARNING: a list can’t be encoded with this method and then decoded using decode_fmt1_from_bytes because the encoding format may be different, but it’s ok to use decode_fmt2_from_bytes

Source

fn fmt2_list_len(&self, bytes: &[u8]) -> Result<usize, String>

Returns the number of elements in an encoded fmt2 list without decoding them

WARNING: This method assumes the bytes were encoded with encode_fmt2_list_to_buf

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§