pub trait EncodeIntoArray {
const MAX_ENCODED_SIZE: usize;
// Required method
fn encode_into_array(&self) -> ([u8; 19], usize);
}Expand description
Trait for encoding values into a stack-allocated array.
This trait provides zero-allocation encoding by using stack-allocated arrays.
All types use a fixed-size array of 19 bytes (enough for any varint), with
each type documenting its actual maximum size via MAX_ENCODED_SIZE.
§Performance
This trait is optimized for embedded and no_std contexts:
- No heap allocations
- Compile-time sized buffers
- Inline hints for hot paths
- Deterministic performance
§Thread Safety
This trait is Send + Sync safe. All implementations are stateless and can
be called concurrently from multiple threads.
§Maximum Encoded Sizes
The maximum varint-encoded size for each type is:
bool,u8: 2 bytesu16: 3 bytesu32: 5 bytesu64: 10 bytesu128: 19 bytesusize: 10 bytes (64-bit) or 5 bytes (32-bit)
§Examples
use multi_trait::EncodeIntoArray;
// Encode a value to a stack array
let (array, len) = 42u8.encode_into_array();
assert_eq!(&array[..len], &[42]);
// Maximum size is known at compile time
assert_eq!(<u32 as EncodeIntoArray>::MAX_ENCODED_SIZE, 5);§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 Associated Constants§
Sourceconst MAX_ENCODED_SIZE: usize
const MAX_ENCODED_SIZE: usize
The maximum number of bytes needed to encode any value of this type.
This is a compile-time constant that documents the maximum size for this specific type, though all types return a 19-byte array.
Required Methods§
Sourcefn encode_into_array(&self) -> ([u8; 19], usize)
fn encode_into_array(&self) -> ([u8; 19], usize)
Encode this value into a stack-allocated array.
Returns a tuple of:
- A 19-byte array containing the encoded bytes (may have unused space)
- The actual length of the encoded data (≤
MAX_ENCODED_SIZE)
§Performance
This method performs no heap allocations. All data is on the stack.
§Examples
use multi_trait::EncodeIntoArray;
let (array, len) = 42u8.encode_into_array();
assert_eq!(len, 1);
assert_eq!(&array[..len], &[42]);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl EncodeIntoArray for bool
Encode a bool into a stack-allocated array
impl EncodeIntoArray for bool
Encode a bool into a stack-allocated array
const MAX_ENCODED_SIZE: usize = 1
fn encode_into_array(&self) -> ([u8; 19], usize)
Source§impl EncodeIntoArray for u8
Encode a u8 into a stack-allocated array
impl EncodeIntoArray for u8
Encode a u8 into a stack-allocated array
const MAX_ENCODED_SIZE: usize = 2
fn encode_into_array(&self) -> ([u8; 19], usize)
Source§impl EncodeIntoArray for u16
Encode a u16 into a stack-allocated array
impl EncodeIntoArray for u16
Encode a u16 into a stack-allocated array
const MAX_ENCODED_SIZE: usize = 3
fn encode_into_array(&self) -> ([u8; 19], usize)
Source§impl EncodeIntoArray for u32
Encode a u32 into a stack-allocated array
impl EncodeIntoArray for u32
Encode a u32 into a stack-allocated array
const MAX_ENCODED_SIZE: usize = 5
fn encode_into_array(&self) -> ([u8; 19], usize)
Source§impl EncodeIntoArray for u64
Encode a u64 into a stack-allocated array
impl EncodeIntoArray for u64
Encode a u64 into a stack-allocated array
const MAX_ENCODED_SIZE: usize = 10
fn encode_into_array(&self) -> ([u8; 19], usize)
Source§impl EncodeIntoArray for u128
Encode a u128 into a stack-allocated array
impl EncodeIntoArray for u128
Encode a u128 into a stack-allocated array