pub struct Encoder { /* private fields */ }Expand description
Builder for encoding multiple values into a byte stream.
Encoder provides a convenient API for serializing complex types
by chaining method calls. All integers are encoded in little-endian format.
§Example
ⓘ
let mut enc = Encoder::with_capacity(64);
enc.push_u64(42);
enc.push_string("hello");
enc.push_bool(true);
enc.push_bytes(&[1, 2, 3]);
let bytes = enc.into_vec();Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new encoder with pre-allocated capacity.
Use this when you know the approximate size to avoid reallocations.
Sourcepub fn push_bytes(&mut self, bytes: &[u8])
pub fn push_bytes(&mut self, bytes: &[u8])
Appends a byte slice with a u32 length prefix.
Format: [length: u32][data: bytes]
Sourcepub fn push_string(&mut self, value: &str)
pub fn push_string(&mut self, value: &str)
Appends a string with a u32 length prefix.
The string is encoded as UTF-8 bytes.
Sourcepub fn push_codec<T: BytesCodec>(&mut self, value: &T)
pub fn push_codec<T: BytesCodec>(&mut self, value: &T)
Appends a value that implements BytesCodec with a length prefix.
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