Trait VarInt

Source
pub trait VarInt:
    TryFrom<u64>
    + Copy
    + Sealed {
    const LOWER_BOUND: usize;
    const UPPER_BOUND: usize;

    // Provided methods
    fn varint_len(self) -> usize { ... }
    fn read<R>(r: &mut R) -> Result<Self, Error>
       where R: Read { ... }
    fn write<W>(varint: &Self, w: &mut W) -> Result<(), Error>
       where W: Write { ... }
}
Expand description

A trait for a number readable/writable as a VarInt.

This is sealed to prevent unintended implementations. It MUST only be implemented for primitive types (or sufficiently approximate types like NonZero<_>).

Required Associated Constants§

Source

const LOWER_BOUND: usize

The lower bound on the amount of bytes this will take up when encoded.

Source

const UPPER_BOUND: usize

The upper bound on the amount of bytes this will take up when encoded.

Provided Methods§

Source

fn varint_len(self) -> usize

The amount of bytes this number will take when serialized as a VarInt.

Source

fn read<R>(r: &mut R) -> Result<Self, Error>
where R: Read,

Read a canonically-encoded VarInt.

Source

fn write<W>(varint: &Self, w: &mut W) -> Result<(), Error>
where W: Write,

Encode a number as a VarInt.

This doesn’t accept self to force writing it as VarInt::write, making it clear it’s being written with the VarInt encoding.

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 u8

Source§

const LOWER_BOUND: usize = 1usize

Source§

const UPPER_BOUND: usize = 2usize

Source§

impl VarInt for u32

Source§

const LOWER_BOUND: usize = 1usize

Source§

const UPPER_BOUND: usize = 5usize

Source§

impl VarInt for u64

Source§

const LOWER_BOUND: usize = 1usize

Source§

const UPPER_BOUND: usize = 10usize

Source§

impl VarInt for usize

Source§

const LOWER_BOUND: usize = 1usize

Source§

const UPPER_BOUND: usize = 10usize

Implementors§