Skip to main content

VarInt

Struct VarInt 

Source
pub struct VarInt;
Expand description

Variable length integer encoding.

Performance note: variable length integer encoding will hurt serialization and deserialization performance significantly relative to fixed width integer encoding. Additionally, all zero-copy capabilities on integers will be lost. Variable length integer encoding may be beneficial if reducing the resulting size of serialized data is important, but if serialization / deserialization performance is important, fixed width integer encoding is highly recommended.

Encoding an unsigned integer v (of any type excepting u8) works as follows:

  1. If u < 251, encode it as a single byte with that value.
  2. If 251 <= u < 2**16, encode it as a literal byte 251, followed by a u16 with value u.
  3. If 2**16 <= u < 2**32, encode it as a literal byte 252, followed by a u32 with value u.
  4. If 2**32 <= u < 2**64, encode it as a literal byte 253, followed by a u64 with value u.
  5. If 2**64 <= u < 2**128, encode it as a literal byte 254, followed by a u128 with value u.

Then, for signed integers, we first convert to unsigned using the zigzag algorithm, and then encode them as we do for unsigned integers generally. The reason we use this algorithm is that it encodes those values which are close to zero in less bytes; the obvious algorithm, where we encode the cast values, gives a very large encoding for all negative values.

The zigzag algorithm is defined as follows:

fn zigzag(v: Signed) -> Unsigned {
    match v {
        0 => 0,
        // To avoid the edge case of Signed::min_value()
        // !n is equal to `-n - 1`, so this is:
        // !n * 2 + 1 = 2(-n - 1) + 1 = -2n - 2 + 1 = -2n - 1
        v if v < 0 => !(v as Unsigned) * 2 + 1,
        v if v > 0 => (v as Unsigned) * 2,
    }
}

And works such that:

assert_eq!(zigzag(0), 0);
assert_eq!(zigzag(-1), 1);
assert_eq!(zigzag(1), 2);
assert_eq!(zigzag(-2), 3);
assert_eq!(zigzag(2), 4);
// etc
assert_eq!(zigzag(i64::min_value()), u64::max_value());

Trait Implementations§

Source§

impl Clone for VarInt

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for VarInt

Source§

impl Debug for VarInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for VarInt

Source§

impl<B: ByteOrder> IntEncoding<B> for VarInt

Source§

const STATIC: bool = false

Whether the encoded length for all integer types T is constant and equal to size_of::<T>(). Read more
Source§

const ZERO_COPY: bool = false

Whether the encoding format for integer types T matches their in-memory representation. Read more
Source§

fn size_of_u16(val: u16) -> usize

Get the encoded size of the given u16 value. Read more
Source§

fn decode_u16<'de>(reader: impl Reader<'de>) -> ReadResult<u16>

Decode a u16 value from the reader.
Source§

fn encode_u16(val: u16, writer: impl Writer) -> WriteResult<()>

Encode the given u16 value and write it to the writer.
Source§

fn size_of_u32(val: u32) -> usize

Get the encoded size of the given u32 value. Read more
Source§

fn decode_u32<'de>(reader: impl Reader<'de>) -> ReadResult<u32>

Decode a u32 value from the reader.
Source§

fn encode_u32(val: u32, writer: impl Writer) -> WriteResult<()>

Encode a u32 value and write it to the writer.
Source§

fn size_of_u64(val: u64) -> usize

Get the encoded size of the given u64 value. Read more
Source§

fn decode_u64<'de>(reader: impl Reader<'de>) -> ReadResult<u64>

Decode a u64 value from the reader.
Source§

fn encode_u64(val: u64, writer: impl Writer) -> WriteResult<()>

Encode a u64 value and write it to the writer.
Source§

fn size_of_u128(val: u128) -> usize

Get the encoded size of the given u128 value. Read more
Source§

fn decode_u128<'de>(reader: impl Reader<'de>) -> ReadResult<u128>

Decode a u128 value from the reader.
Source§

fn encode_u128(val: u128, writer: impl Writer) -> WriteResult<()>

Encode a u128 value and write it to the writer.
Source§

fn size_of_i16(val: i16) -> usize

Get the encoded size of the given i16 value. Read more
Source§

fn size_of_i32(val: i32) -> usize

Get the encoded size of the given i32 value. Read more
Source§

fn size_of_i64(val: i64) -> usize

Get the encoded size of the given i64 value. Read more
Source§

fn size_of_i128(val: i128) -> usize

Get the encoded size of the given i128 value. Read more
Source§

fn encode_i16(val: i16, writer: impl Writer) -> WriteResult<()>

Encode a i16 value and write it to the writer.
Source§

fn encode_i32(val: i32, writer: impl Writer) -> WriteResult<()>

Encode a i32 value and write it to the writer.
Source§

fn encode_i64(val: i64, writer: impl Writer) -> WriteResult<()>

Encode a i64 value and write it to the writer.
Source§

fn encode_i128(val: i128, writer: impl Writer) -> WriteResult<()>

Encode a i128 value and write it to the writer.
Source§

fn decode_i16<'de>(reader: impl Reader<'de>) -> ReadResult<i16>

Decode a i16 value from the reader.
Source§

fn decode_i32<'de>(reader: impl Reader<'de>) -> ReadResult<i32>

Decode a i32 value from the reader.
Source§

fn decode_i64<'de>(reader: impl Reader<'de>) -> ReadResult<i64>

Decode a i64 value from the reader.
Source§

fn decode_i128<'de>(reader: impl Reader<'de>) -> ReadResult<i128>

Decode a i128 value from the reader.
Source§

impl PartialEq for VarInt

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for VarInt

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.