Trait Serializable

Source
pub trait Serializable {
    // Required methods
    fn serialize(&self) -> Vec<u8> 
       where Self: Sized;
    fn deserialize_as_a_unit(
        bytes: &[u8],
        begin: &mut usize,
    ) -> Result<Self, DecodingError>
       where Self: Sized;

    // Provided method
    fn deserialize(bytes: &[u8]) -> Result<Self, DecodingError>
       where Self: Sized { ... }
}
Expand description

Trait for encoding.

Required Methods§

Source

fn serialize(&self) -> Vec<u8>
where Self: Sized,

Encode the input object.

Source

fn deserialize_as_a_unit( bytes: &[u8], begin: &mut usize, ) -> Result<Self, DecodingError>
where Self: Sized,

Decode some of the input bytes starting from the begin position as a Self object, possibly with some bytes at the end left.

Note that bytes is the input bytes to be decoded, and begin is the beginning position of bytes. At the end of the execution, begin should point to the first byte not decoded.

Provided Methods§

Source

fn deserialize(bytes: &[u8]) -> Result<Self, DecodingError>
where Self: Sized,

Decode the input bytes as a Self object, using up all bytes.

The default implementation of this method is to first call deserialize_as_a_unit with begin = 0. If any error message is returned, return the error message directly. If begin != bytes.len(), which means there are bytes not used for decoding, return DecodingError::TooManyEncodedBytes. Otherwise, return the object of decoding result.

Implementors§