integer_encoding/lib.rs
1//! Fast serialization of integers.
2//!
3//! This crate implements encoding and decoding of integer types to and from `FixedInt` (i.e. a
4//! representation of integers similar or equal to how they are stored in memory) as well as
5//! `VarInt` (encoding integers so that they only use as much memory as needed to represent their
6//! magnitude).
7//!
8//! This is useful when (de)serializing data from and to binary representations. For example,
9//! Protocol Buffers (by Google) use these kinds of encoding.
10//!
11//! ```
12//! use integer_encoding::*;
13//!
14//! fn main() {
15//! let a: u32 = 344;
16//! let encoded_byte_slice = a.encode_fixed_light();
17//! assert_eq!(a, u32::decode_fixed(encoded_byte_slice));
18//! assert_eq!(4, encoded_byte_slice.len());
19//!
20//! let b: i32 = -111;
21//! let encoded_byte_vec = b.encode_var_vec();
22//! assert_eq!(Some((b, 2)), i32::decode_var(&encoded_byte_vec));
23//! }
24//! ```
25
26mod fixed;
27mod fixed_tests;
28
29mod varint;
30mod varint_tests;
31
32mod reader;
33mod writer;
34
35pub use fixed::FixedInt;
36pub use varint::VarInt;
37
38#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
39pub use reader::FixedIntAsyncReader;
40pub use reader::FixedIntReader;
41#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
42pub use reader::VarIntAsyncReader;
43pub use reader::VarIntReader;
44
45#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
46pub use writer::FixedIntAsyncWriter;
47pub use writer::FixedIntWriter;
48#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
49pub use writer::VarIntAsyncWriter;
50pub use writer::VarIntWriter;