Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

In-memory encoder. Writes into an owned Vec<u8>; the buffer can be reused across encodes by calling Encoder::take to swap it out.

Implements Encode, so Serialize impls written generically over E: Encode work directly through it.

§Examples

use pack_io::Encoder;

let mut enc = Encoder::new();
enc.write(&7_u64).unwrap();
enc.write(&"hello").unwrap();
let bytes = enc.into_inner();
assert!(bytes.len() > 0);

Implementations§

Source§

impl Encoder

Source

pub fn new() -> Self

Construct an encoder with an empty output buffer.

§Examples
let enc = pack_io::Encoder::new();
assert!(enc.as_bytes().is_empty());
Source

pub fn into_buffer(buffer: Vec<u8>) -> Self

Construct an encoder backed by buffer. The encoder appends to the buffer rather than allocating its own — callers that re-use a single Vec<u8> across many encodes avoid the per-call allocation.

§Examples
use pack_io::Encoder;

let buf = Vec::with_capacity(64);
let mut enc = Encoder::into_buffer(buf);
enc.write(&42_u64).unwrap();
let buf = enc.into_inner();
assert!(!buf.is_empty());
Source

pub fn as_bytes(&self) -> &[u8]

Borrow the encoded bytes accumulated so far.

Source

pub fn into_inner(self) -> Vec<u8>

Consume the encoder and return its underlying buffer.

Source

pub fn take(&mut self) -> Vec<u8>

Swap the encoder’s buffer with a fresh empty one, returning the bytes written so far. Useful for “encode then send” loops that want to re-use the encoder.

Source

pub fn write<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<()>

Encode value, appending its bytes to the internal buffer.

§Errors

Propagates any error returned by the type’s Serialize implementation. Primitive impls in this crate never error on an in-memory encoder.

Trait Implementations§

Source§

impl Debug for Encoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Encoder

Source§

fn default() -> Encoder

Returns the “default value” for a type. Read more
Source§

impl Encode for Encoder

Source§

fn write_byte(&mut self, byte: u8) -> Result<()>

Append a single byte. Read more
Source§

fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>

Append a slice of bytes. Read more
Source§

fn reserve(&mut self, additional: usize)

Hint that the caller is about to write additional more bytes. Read more
Source§

fn write_varint_u64(&mut self, value: u64) -> Result<()>

Append a u64 as an unsigned LEB128 varint (1–10 bytes). Read more
Source§

fn write_varint_u128(&mut self, value: u128) -> Result<()>

Append a u128 as an unsigned LEB128 varint (1–19 bytes). Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.