pub trait EncodeInto {
// Required method
fn encode_into(&self) -> Vec<u8> ⓘ;
}Expand description
Trait for encoding values into compact varint byte representation.
This trait provides efficient encoding of numeric values into unsigned varint
format, which uses fewer bytes for smaller values. The encoding is deterministic
and reversible via TryDecodeFrom.
§Varint Format
The varint (variable-length integer) format uses the most significant bit (MSB) of each byte as a continuation bit:
- If MSB is 1, more bytes follow
- If MSB is 0, this is the last byte
This allows small values to use fewer bytes:
- Values 0-127: 1 byte
- Values 128-16,383: 2 bytes
- And so on…
§Performance
Encoding is optimized for performance:
- Single heap allocation per call
- O(1) length calculation
- No byte-by-byte copying
§Thread Safety
This trait is Send + Sync safe. All implementations are stateless and can
be called concurrently from multiple threads.
§Examples
use multi_trait::EncodeInto;
// Small values use minimal space
let small = 42u8;
let encoded = small.encode_into();
assert_eq!(encoded.len(), 1);
// Larger values use more bytes as needed
let large = 256u16;
let encoded = large.encode_into();
assert!(encoded.len() > 1);
// Boolean encoding
assert_eq!(true.encode_into(), vec![1]);
assert_eq!(false.encode_into(), vec![0]);§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(&self) -> Vec<u8> ⓘ
fn encode_into(&self) -> Vec<u8> ⓘ
Encode this value into a compact varint Vec<u8>.
This method allocates a new Vec containing the encoded bytes.
The resulting vector’s length depends on the value’s magnitude.
§Returns
A Vec<u8> containing the varint-encoded representation of this value.
§Examples
use multi_trait::EncodeInto;
let value = 300u16;
let bytes = value.encode_into();
// The exact bytes depend on varint encoding rules
assert!(!bytes.is_empty());Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl EncodeInto for bool
Encode a bool into a compact varuint Vec<u8>
impl EncodeInto for bool
Encode a bool into a compact varuint Vec<u8>
Source§impl EncodeInto for u8
Encode a u8 into a compact varuint Vec<u8>
impl EncodeInto for u8
Encode a u8 into a compact varuint Vec<u8>
Source§impl EncodeInto for u16
Encode a u16 into a compact varuint Vec<u8>
impl EncodeInto for u16
Encode a u16 into a compact varuint Vec<u8>
Source§impl EncodeInto for u32
Encode a u32 into a compact varuint Vec<u8>
impl EncodeInto for u32
Encode a u32 into a compact varuint Vec<u8>
Source§impl EncodeInto for u64
Encode a u64 into a compact varuint Vec<u8>
impl EncodeInto for u64
Encode a u64 into a compact varuint Vec<u8>
Source§impl EncodeInto for u128
Encode a u128 into a compact varuint Vec<u8>
impl EncodeInto for u128
Encode a u128 into a compact varuint Vec<u8>
Source§impl EncodeInto for usize
Encode a usize into a compact varuint Vec<u8>
impl EncodeInto for usize
Encode a usize into a compact varuint Vec<u8>
Source§impl<const N: usize> EncodeInto for [u8; N]
Encode a fixed-length byte array as raw bytes (used for BLS share identifiers).
impl<const N: usize> EncodeInto for [u8; N]
Encode a fixed-length byte array as raw bytes (used for BLS share identifiers).