pub trait EncodeIntoBuffer {
// Required method
fn encode_into_buffer(&self, buffer: &mut Vec<u8>);
}Expand description
Trait for encoding values into an existing buffer.
This trait provides zero-allocation encoding by appending to an existing
Vec<u8>. This is more efficient than EncodeInto
when:
- Encoding multiple values sequentially
- Working in allocation-sensitive contexts
- Reusing buffers across multiple operations
§Performance
This trait is optimized for minimal overhead:
- No heap allocations (unless buffer needs to grow)
- Direct buffer manipulation
- Inline hints for hot paths
§Thread Safety
This trait is Send + Sync safe. All implementations are stateless and can
be called concurrently from multiple threads.
§Examples
use multi_trait::EncodeIntoBuffer;
// Create a reusable buffer
let mut buffer = Vec::with_capacity(64);
// Encode values without allocation
42u8.encode_into_buffer(&mut buffer);
1000u16.encode_into_buffer(&mut buffer);
// Buffer contains both encoded values
assert!(buffer.len() >= 2);§Implemented For
bool: Encoded as 0 (false) or 1 (true)u8,u16,u32,u64,u128: Variable-length encodingusize: Platform-dependent (32-bit or 64-bit)
Required Methods§
Sourcefn encode_into_buffer(&self, buffer: &mut Vec<u8>)
fn encode_into_buffer(&self, buffer: &mut Vec<u8>)
Encode this value and append it to the given buffer.
The buffer will be extended with the varint-encoded bytes representing this value. The buffer’s existing contents are preserved.
§Performance
This method is optimized to minimize allocations. If the buffer has sufficient capacity, no allocation occurs. Otherwise, the buffer will grow according to Vec’s growth strategy.
§Examples
use multi_trait::EncodeIntoBuffer;
let mut buffer = Vec::new();
42u8.encode_into_buffer(&mut buffer);
assert_eq!(buffer, vec![42]);
// Append another value
100u8.encode_into_buffer(&mut buffer);
assert_eq!(buffer.len(), 2);Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl EncodeIntoBuffer for bool
Encode a bool and append to a buffer
impl EncodeIntoBuffer for bool
Encode a bool and append to a buffer
fn encode_into_buffer(&self, buffer: &mut Vec<u8>)
Source§impl EncodeIntoBuffer for u8
Encode a u8 and append to a buffer
impl EncodeIntoBuffer for u8
Encode a u8 and append to a buffer
fn encode_into_buffer(&self, buffer: &mut Vec<u8>)
Source§impl EncodeIntoBuffer for u16
Encode a u16 and append to a buffer
impl EncodeIntoBuffer for u16
Encode a u16 and append to a buffer
fn encode_into_buffer(&self, buffer: &mut Vec<u8>)
Source§impl EncodeIntoBuffer for u32
Encode a u32 and append to a buffer
impl EncodeIntoBuffer for u32
Encode a u32 and append to a buffer
fn encode_into_buffer(&self, buffer: &mut Vec<u8>)
Source§impl EncodeIntoBuffer for u64
Encode a u64 and append to a buffer
impl EncodeIntoBuffer for u64
Encode a u64 and append to a buffer
fn encode_into_buffer(&self, buffer: &mut Vec<u8>)
Source§impl EncodeIntoBuffer for u128
Encode a u128 and append to a buffer
impl EncodeIntoBuffer for u128
Encode a u128 and append to a buffer
fn encode_into_buffer(&self, buffer: &mut Vec<u8>)
Source§impl EncodeIntoBuffer for usize
Encode a usize and append to a buffer
impl EncodeIntoBuffer for usize
Encode a usize and append to a buffer