pub trait CompactEncoding<Decode: ?Sized = Self> {
// Required methods
fn encoded_size(&self) -> Result<usize, EncodingError>;
fn encode<'a>(
&self,
buffer: &'a mut [u8],
) -> Result<&'a mut [u8], EncodingError>;
fn decode(buffer: &[u8]) -> Result<(Decode, &[u8]), EncodingError>
where Decode: Sized;
// Provided methods
fn to_encoded_bytes(&self) -> Result<Box<[u8]>, EncodingError> { ... }
fn create_buffer(&self) -> Result<Box<[u8]>, EncodingError> { ... }
fn encode_with_len<'a>(
&self,
buffer: &'a mut [u8],
) -> Result<(&'a mut [u8], usize), EncodingError> { ... }
fn decode_with_len(
buffer: &[u8],
) -> Result<(Decode, &[u8], usize), EncodingError>
where Decode: Sized { ... }
}
Expand description
A trait for building small and fast parsers and serializers.
Required Methods§
Sourcefn encoded_size(&self) -> Result<usize, EncodingError>
fn encoded_size(&self) -> Result<usize, EncodingError>
The size in bytes required to encode self
.
Provided Methods§
Sourcefn to_encoded_bytes(&self) -> Result<Box<[u8]>, EncodingError>
fn to_encoded_bytes(&self) -> Result<Box<[u8]>, EncodingError>
Encode self
into a Vec<u8>
. This is just a helper method for creating a buffer and
encoding to it in one step.
let foo: Ipv4Addr = "0.0.0.0".parse()?;
let mut buff = vec![0; foo.encoded_size()?];
foo.encode(&mut buff)?;
Sourcefn create_buffer(&self) -> Result<Box<[u8]>, EncodingError>
fn create_buffer(&self) -> Result<Box<[u8]>, EncodingError>
Create an empty buffer of the correct size for encoding self
to. This is just a helper
method for: encoding to it in one step.
let foo: Ipv4Addr = "0.0.0.0".parse()?;
vec![0; foo.encoded_size()?];
Sourcefn encode_with_len<'a>(
&self,
buffer: &'a mut [u8],
) -> Result<(&'a mut [u8], usize), EncodingError>
fn encode_with_len<'a>( &self, buffer: &'a mut [u8], ) -> Result<(&'a mut [u8], usize), EncodingError>
Like CompactEncoding::encode
but also return the number of bytes encoded.
Sourcefn decode_with_len(
buffer: &[u8],
) -> Result<(Decode, &[u8], usize), EncodingError>where
Decode: Sized,
fn decode_with_len(
buffer: &[u8],
) -> Result<(Decode, &[u8], usize), EncodingError>where
Decode: Sized,
Like CompactEncoding::decode
but also return the number of bytes decoded.
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.