pub trait Encodable: Send + Sync {
type Error: Error + Send + Sync + 'static;
// Required methods
fn encode(&self, dst: &mut [u8]) -> Result<usize, Self::Error>;
fn encoded_len(&self) -> usize;
// Provided methods
fn encode_to_vec(&self) -> Result<Vec<u8>, Self::Error> { ... }
fn encode_to_writer<W: Write>(&self, writer: &mut W) -> Result<usize> { ... }
fn encode_to_async_writer<W: AsyncWrite + Send + Unpin>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<usize>> + Send { ... }
}Expand description
The type can transform its representation to byte form.
Required Associated Types§
Required Methods§
Sourcefn encode(&self, dst: &mut [u8]) -> Result<usize, Self::Error>
fn encode(&self, dst: &mut [u8]) -> Result<usize, Self::Error>
Encodes the value into the given buffer for transmission.
Returns the number of bytes written to the buffer.
Sourcefn encoded_len(&self) -> usize
fn encoded_len(&self) -> usize
Returns the encoded length of the value. This is used to pre-allocate a buffer for encoding.
Provided Methods§
Sourcefn encode_to_vec(&self) -> Result<Vec<u8>, Self::Error>
Available on crate features alloc or std only.
fn encode_to_vec(&self) -> Result<Vec<u8>, Self::Error>
alloc or std only.Encodes the value into a vec for transmission.
Sourcefn encode_to_writer<W: Write>(&self, writer: &mut W) -> Result<usize>
Available on crate feature std only.
fn encode_to_writer<W: Write>(&self, writer: &mut W) -> Result<usize>
std only.Encodes the value into the given writer for transmission.
Sourcefn encode_to_async_writer<W: AsyncWrite + Send + Unpin>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<usize>> + Send
Available on crate feature async only.
fn encode_to_async_writer<W: AsyncWrite + Send + Unpin>( &self, writer: &mut W, ) -> impl Future<Output = Result<usize>> + Send
async only.Encodes the value into the given async writer for transmission.
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.