#serialization #integer #varint #protobuf #zigzag #signed-integer

integer-encoding

varint+zigzag and fixedint integer encoding/decoding (https://developers.google.com/protocol-buffers/docs/encoding)

31 stable releases (4 major)

4.0.2 Aug 1, 2024
4.0.0 Jun 30, 2023
3.0.4 Jun 22, 2022
3.0.3 Feb 22, 2022
0.12.0 Jun 19, 2016

#79 in Encoding

Download history 357018/week @ 2024-07-29 344919/week @ 2024-08-05 364951/week @ 2024-08-12 348165/week @ 2024-08-19 355452/week @ 2024-08-26 337367/week @ 2024-09-02 327186/week @ 2024-09-09 311416/week @ 2024-09-16 323886/week @ 2024-09-23 321677/week @ 2024-09-30 295377/week @ 2024-10-07 285878/week @ 2024-10-14 279305/week @ 2024-10-21 254639/week @ 2024-10-28 260017/week @ 2024-11-04 249877/week @ 2024-11-11

1,056,063 downloads per month
Used in 888 crates (96 directly)

MIT license

35KB
720 lines

integer-encoding-rs

crates.io Actions CI

full documentation

This crate provides encoding and decoding of integers to and from bytestring representations.

The format is described here: Google's protobuf integer encoding technique.

Please feel free to use cargo bench to determine the rate at which your machine can encode and decode varints and fixedints. Note that one iteration comprises each eight rounds of encoding (or decoding) a signed and an unsigned integer each -- divide the resulting benchmark time by 16 in order to have a rough estimate of time per operation. The integers are very large, so the results represent the worst case.

Crate

If you use Tokio v0.2 and you use the asynchronous types in this crate (feature tokio_async), you may be interested in the v2.0 branch. It is still maintained with the occasional fix for edge cases and depends on Tokio v0.2.

FixedInt

FixedInt casts integers to bytes by either copying the underlying memory or performing a transmutation. The encoded values use are little-endian.

However, a trait method is implemented for all integer types allowing convenient conversion between little and big endian. That is, if you receive a big-endian on the wire and decode it, it will first be interpreted as little-endian; converting will recover the correct value.

VarInt

VarInt encodes integers in blocks of 7 bits; the MSB is set for every byte but the last, in which it is cleared.

Signed values are first converted to an unsigned representation using zigzag encoding (also described on the page linked above), and then encoded as every other unsigned number.

Dependencies

~0–6.5MB
~25K SLoC