27 stable releases (3 major)

3.0.2 Feb 15, 2021
2.1.3 Feb 15, 2021
2.1.1 Oct 22, 2020
1.2.2 Jan 10, 2021
0.12.0 Jun 19, 2016

#27 in Encoding

Download history 8403/week @ 2020-12-22 10874/week @ 2020-12-29 18406/week @ 2021-01-05 19664/week @ 2021-01-12 19717/week @ 2021-01-19 23157/week @ 2021-01-26 25200/week @ 2021-02-02 21946/week @ 2021-02-09 25683/week @ 2021-02-16 27128/week @ 2021-02-23 23214/week @ 2021-03-02 25840/week @ 2021-03-09 22160/week @ 2021-03-16 26727/week @ 2021-03-23 21905/week @ 2021-03-30 22895/week @ 2021-04-06

83,766 downloads per month
Used in 116 crates (26 directly)

MIT license

32KB
660 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 machine endianness (little-endian on x86).

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–740KB
~13K SLoC