pub trait Encode {
// Required methods
fn write_byte(&mut self, byte: u8) -> Result<()>;
fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>;
// Provided methods
fn reserve(&mut self, additional: usize) { ... }
fn write_varint_u64(&mut self, value: u64) -> Result<()> { ... }
fn write_varint_u128(&mut self, value: u128) -> Result<()> { ... }
}Expand description
Sink that a Serialize implementation writes its wire-format bytes
into.
Implemented by every concrete encoder in the crate (Encoder for the
in-memory case, IoEncoder (feature std) for std::io::Write streams). User
code rarely implements Encode directly — Serialize impls are written
generically over E: Encode so a single impl works for every encoder
flavour.
§Examples
use pack_io::{Encode, Encoder, Result};
// A helper that writes a length-prefixed list of `u32`s into any encoder.
fn write_u32_list<E: Encode>(enc: &mut E, items: &[u32]) -> Result<()> {
enc.write_varint_u64(items.len() as u64)?;
for item in items {
enc.write_varint_u64(u64::from(*item))?;
}
Ok(())
}
let mut enc = Encoder::new();
write_u32_list(&mut enc, &[1, 2, 3]).unwrap();Required Methods§
Sourcefn write_byte(&mut self, byte: u8) -> Result<()>
fn write_byte(&mut self, byte: u8) -> Result<()>
Sourcefn write_bytes(&mut self, bytes: &[u8]) -> Result<()>
fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>
Provided Methods§
Sourcefn reserve(&mut self, additional: usize)
fn reserve(&mut self, additional: usize)
Hint that the caller is about to write additional more bytes.
In-memory encoders MAY pre-allocate the requested capacity to avoid
intermediate Vec growth. Streaming encoders typically ignore the
hint. The default implementation is a no-op.
Sourcefn write_varint_u64(&mut self, value: u64) -> Result<()>
fn write_varint_u64(&mut self, value: u64) -> Result<()>
Sourcefn write_varint_u128(&mut self, value: u128) -> Result<()>
fn write_varint_u128(&mut self, value: u128) -> Result<()>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl Encode for BytesMut
Available on crate feature bytes only.Appends to the buffer, growing it as needed. Never fails.
impl Encode for BytesMut
bytes only.Appends to the buffer, growing it as needed. Never fails.