#byte #varint #protobuf #integer-value

bytes-varint

variable-length integer encoding (protobuf-style) for the bytes crate

4 stable releases

1.0.3 Mar 22, 2023

#668 in Encoding

Download history 31104/week @ 2023-12-14 13527/week @ 2023-12-21 16851/week @ 2023-12-28 33453/week @ 2024-01-04 38309/week @ 2024-01-11 41780/week @ 2024-01-18 33473/week @ 2024-01-25 35688/week @ 2024-02-01 40235/week @ 2024-02-08 36558/week @ 2024-02-15 50878/week @ 2024-02-22 48459/week @ 2024-02-29 42071/week @ 2024-03-07 42369/week @ 2024-03-14 43174/week @ 2024-03-21 40136/week @ 2024-03-28

178,378 downloads per month
Used in lz4_net_legacy

MIT license

22KB
306 lines

bytes-varint

This crate extends the bytes crate with support for variable-length serialization and deserialization of integer values (protobuf style).

Seamless integration with bytes

This crate is not affiliated with the bytes crate, but it integrates seamlessly by providing blanket implementations for bytes::Buf / bytes::BufMut.

Importing bytes_varint::* makes varint functions available on Buf / BufMut instances:

use bytes_varint::*;

fn put_numbers(buf: &mut impl BufMut, i: i16, j: u64) {
    buf.put_i16_varint(i);
    buf.put_u64_varint(j);
}

fn get_number(buf: &mut impl Buf) -> VarIntResult<u32> {
    buf.get_u32_varint()
}

Failure Modes

Variable-length decoding can fail, and callers have no way of performing checks up-front to ensure success. This is different from fixed-length decoding that is guaranteed to succeed if e.g. the buffer has at least four available bytes when decoding an i32.

There are two failure modes:

  • numeric overflow - the encoding has no inherent upper bound on the number of bits in a number, so a decoded number may be too large to fit into a given numeric primitive type
  • buffer underflow - there is no way to know in advance how many bytes will be read when decoding a number. So callers can not check in advance, and decoding can fail.

Algorithm

Variable-length encoding (see https://en.wikipedia.org/wiki/Variable-length_quantity for details and trade-offs) stores a number in a sequence of bytes, using each byte's seven least significant bits storing actual data, and the most significant bit specifying if there are more bytes to come. This allows small numbers to be stored in a single byte regardless of the raw value's number of bits.

Signed integers are 'zig-zag' encoded (https://developers.google.com/protocol-buffers/docs/encoding#types), mapping the range of -64 to 63 to a single byte.

Dependencies

~170KB