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:
- If
u < 251, encode it as a single byte with that value. - If
251 <= u < 2**16, encode it as a literal byte 251, followed by a u16 with valueu. - If
2**16 <= u < 2**32, encode it as a literal byte 252, followed by a u32 with valueu. - If
2**32 <= u < 2**64, encode it as a literal byte 253, followed by a u64 with valueu. - If
2**64 <= u < 2**128, encode it as a literal byte 254, followed by a u128 with valueu.
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§
impl Copy for VarInt
impl Eq for VarInt
Source§impl<B: ByteOrder> IntEncoding<B> for VarInt
impl<B: ByteOrder> IntEncoding<B> for VarInt
Source§const STATIC: bool = false
const STATIC: bool = false
T is constant and equal
to size_of::<T>(). Read moreSource§const ZERO_COPY: bool = false
const ZERO_COPY: bool = false
T matches their in-memory representation. Read moreSource§fn decode_u16<'de>(reader: impl Reader<'de>) -> ReadResult<u16>
fn decode_u16<'de>(reader: impl Reader<'de>) -> ReadResult<u16>
u16 value from the reader.Source§fn encode_u16(val: u16, writer: impl Writer) -> WriteResult<()>
fn encode_u16(val: u16, writer: impl Writer) -> WriteResult<()>
u16 value and write it to the writer.Source§fn decode_u32<'de>(reader: impl Reader<'de>) -> ReadResult<u32>
fn decode_u32<'de>(reader: impl Reader<'de>) -> ReadResult<u32>
u32 value from the reader.Source§fn encode_u32(val: u32, writer: impl Writer) -> WriteResult<()>
fn encode_u32(val: u32, writer: impl Writer) -> WriteResult<()>
u32 value and write it to the writer.Source§fn decode_u64<'de>(reader: impl Reader<'de>) -> ReadResult<u64>
fn decode_u64<'de>(reader: impl Reader<'de>) -> ReadResult<u64>
u64 value from the reader.Source§fn encode_u64(val: u64, writer: impl Writer) -> WriteResult<()>
fn encode_u64(val: u64, writer: impl Writer) -> WriteResult<()>
u64 value and write it to the writer.Source§fn decode_u128<'de>(reader: impl Reader<'de>) -> ReadResult<u128>
fn decode_u128<'de>(reader: impl Reader<'de>) -> ReadResult<u128>
u128 value from the reader.Source§fn encode_u128(val: u128, writer: impl Writer) -> WriteResult<()>
fn encode_u128(val: u128, writer: impl Writer) -> WriteResult<()>
u128 value and write it to the writer.Source§fn encode_i16(val: i16, writer: impl Writer) -> WriteResult<()>
fn encode_i16(val: i16, writer: impl Writer) -> WriteResult<()>
i16 value and write it to the writer.Source§fn encode_i32(val: i32, writer: impl Writer) -> WriteResult<()>
fn encode_i32(val: i32, writer: impl Writer) -> WriteResult<()>
i32 value and write it to the writer.Source§fn encode_i64(val: i64, writer: impl Writer) -> WriteResult<()>
fn encode_i64(val: i64, writer: impl Writer) -> WriteResult<()>
i64 value and write it to the writer.Source§fn encode_i128(val: i128, writer: impl Writer) -> WriteResult<()>
fn encode_i128(val: i128, writer: impl Writer) -> WriteResult<()>
i128 value and write it to the writer.Source§fn decode_i16<'de>(reader: impl Reader<'de>) -> ReadResult<i16>
fn decode_i16<'de>(reader: impl Reader<'de>) -> ReadResult<i16>
i16 value from the reader.Source§fn decode_i32<'de>(reader: impl Reader<'de>) -> ReadResult<i32>
fn decode_i32<'de>(reader: impl Reader<'de>) -> ReadResult<i32>
i32 value from the reader.Source§fn decode_i64<'de>(reader: impl Reader<'de>) -> ReadResult<i64>
fn decode_i64<'de>(reader: impl Reader<'de>) -> ReadResult<i64>
i64 value from the reader.Source§fn decode_i128<'de>(reader: impl Reader<'de>) -> ReadResult<i128>
fn decode_i128<'de>(reader: impl Reader<'de>) -> ReadResult<i128>
i128 value from the reader.