Skip to main content

Encode

Trait Encode 

Source
pub trait Encode: Sized {
    // Required method
    fn encode<W: Write>(&self, w: W) -> Result<usize, Error>;

    // Provided methods
    fn type_id(&self) -> Option<u32> { ... }
    fn encode_to_vec(&self) -> Result<Vec<u8>, Error>
       where Self: Sealed { ... }
}
Expand description

A trait that can be encoded into an io::Write stream.

Implementing this trait allows types to be encoded into an io::Write stream, which is useful for writing data to various destinations like files, buffers, and streams.

§Examples

use codeq::Encode;

let data = "hello".to_string();
let mut buf = Vec::new();
data.encode(&mut buf).unwrap();
assert_eq!(buf, b"\x00\x00\x00\x05hello");

Required Methods§

Source

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

Provided Methods§

Source

fn type_id(&self) -> Option<u32>

Returns the leading type id when the encoded form starts with one.

Source

fn encode_to_vec(&self) -> Result<Vec<u8>, Error>
where Self: Sealed,

Encodes the value into a new Vec<u8>.

This method is sealed and cannot be implemented outside of the crate.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Encode for &str

Source§

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

Source§

impl Encode for ()

Source§

fn encode<W: Write>(&self, _w: W) -> Result<usize, Error>

Source§

impl Encode for String

Source§

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

Source§

impl Encode for Vec<u8>

Source§

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

Source§

impl Encode for bool

Source§

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

Source§

impl Encode for u8

Source§

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

Source§

impl Encode for u32

Source§

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

Source§

impl Encode for u64

Source§

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

Source§

impl<A: Encode, B: Encode> Encode for (A, B)

Source§

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

Source§

impl<T: Encode> Encode for &T

Source§

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

Source§

fn type_id(&self) -> Option<u32>

Source§

impl<T: Encode> Encode for Option<T>

Source§

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

Implementors§

Source§

impl<C, T> Encode for WithChecksum<C, T>
where C: CodeqConfig, T: Encode,

Source§

impl<C> Encode for Segment<C>
where C: CodeqConfig,