Varint

Trait Varint 

Source
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§

Source

const MIN_ENCODED_LEN: usize

The minimum number of bytes needed to encode any value of this type.

  • For u16 and i16, this is 1.
  • For u32 and i32, this is 1.
  • For u64 and i64, this is 1.
  • For u128 and i128, this is 1.
Source

const MAX_ENCODED_LEN: usize

The maximum number of bytes that might be needed to encode any value of this type.

  • For u16 and i16, this is 3.
  • For u32 and i32, this is 5.
  • For u64 and i64, this is 10.
  • For u128 and i128, this is 19.

Provided Associated Constants§

Source

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 u16 and i16, this range is 1..=3, representing possible encoded lengths of 1, 2, or 3 bytes.
  • For u32 and i32, this range is 1..=5, representing possible encoded lengths of 1, 2, 3, 4, or 5 bytes.
  • For u64 and u64, this range is 1..=10, representing possible encoded lengths of 1 to 10 bytes.
  • For u128 and i128, this range is 1..=19, representing possible encoded lengths of 1 to 19 bytes.

Required Methods§

Source

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.

Source

fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>

Encodes the value as a varint and writes it to the buffer.

Returns the number of bytes written to the buffer.

Source

fn decode(buf: &[u8]) -> Result<(usize, Self), DecodeError>
where Self: Sized,

Decodes the value from the buffer.

Returns the number of bytes read from the buffer and the decoded value if successful.

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.

Implementations on Foreign Types§

Source§

impl Varint for i16

Source§

impl Varint for i32

Source§

impl Varint for i64

Source§

const MIN_ENCODED_LEN: usize = 1usize

Source§

const MAX_ENCODED_LEN: usize = 10usize

Source§

fn encoded_len(&self) -> usize

Source§

fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>

Source§

fn decode(buf: &[u8]) -> Result<(usize, i64), DecodeError>

Source§

impl Varint for i128

Source§

impl Varint for u16

Source§

impl Varint for u32

Source§

impl Varint for u64

Source§

const MIN_ENCODED_LEN: usize = 1usize

Source§

const MAX_ENCODED_LEN: usize = 10usize

Source§

fn encoded_len(&self) -> usize

Source§

fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>

Source§

fn decode(buf: &[u8]) -> Result<(usize, u64), DecodeError>

Source§

impl Varint for u128

Source§

impl Varint for Duration

Implementors§