pub trait Varint {
const MIN_ENCODED_LEN: usize;
const MAX_ENCODED_LEN: usize;
const ENCODED_LEN_RANGE: RangeInclusive<usize> = _;
// Required methods
fn encoded_len(&self) -> usize;
fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>;
fn decode(buf: &[u8]) -> Result<(usize, Self), DecodeError>
where Self: Sized;
}Expand description
A trait for types that can be encoded as variable-length integers (varints).
Varints are a method of serializing integers using one or more bytes that allows small numbers to be stored in fewer bytes. The encoding scheme is compatible with Protocol Buffers’ base-128 varint format.
Required Associated Constants§
Sourceconst MIN_ENCODED_LEN: usize
const MIN_ENCODED_LEN: usize
The minimum number of bytes needed to encode any value of this type.
- For
u16andi16, this is1. - For
u32andi32, this is1. - For
u64andi64, this is1. - For
u128andi128, this is1.
Sourceconst MAX_ENCODED_LEN: usize
const MAX_ENCODED_LEN: usize
The maximum number of bytes that might be needed to encode any value of this type.
- For
u16andi16, this is3. - For
u32andi32, this is5. - For
u64andi64, this is10. - For
u128andi128, this is19.
Provided Associated Constants§
Sourceconst ENCODED_LEN_RANGE: RangeInclusive<usize> = _
const ENCODED_LEN_RANGE: RangeInclusive<usize> = _
The range of possible encoded lengths for this type, from MIN_ENCODED_LEN to MAX_ENCODED_LEN inclusive.
This range can be used to pre-allocate buffers or validate encoded data lengths.
- For
u16andi16, this range is1..=3, representing possible encoded lengths of 1, 2, or 3 bytes. - For
u32andi32, this range is1..=5, representing possible encoded lengths of 1, 2, 3, 4, or 5 bytes. - For
u64andu64, this range is1..=10, representing possible encoded lengths of 1 to 10 bytes. - For
u128andi128, this range is1..=19, representing possible encoded lengths of 1 to 19 bytes.
Required Methods§
Sourcefn encoded_len(&self) -> usize
fn encoded_len(&self) -> usize
Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range Self::ENCODED_LEN_RANGE.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.