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
impl Encoder
Sourcepub fn new() -> Self
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());Sourcepub fn into_buffer(buffer: Vec<u8>) -> Self
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());Sourcepub fn into_inner(self) -> Vec<u8> ⓘ
pub fn into_inner(self) -> Vec<u8> ⓘ
Consume the encoder and return its underlying buffer.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Encoder
impl RefUnwindSafe for Encoder
impl Send for Encoder
impl Sync for Encoder
impl Unpin for Encoder
impl UnsafeUnpin for Encoder
impl UnwindSafe for Encoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more