Expand description
Fast serialization of integers.
This crate implements encoding and decoding of integer types to and from FixedInt
(i.e. a
representation of integers similar or equal to how they are stored in memory) as well as
VarInt
(encoding integers so that they only use as much memory as needed to represent their
magnitude).
This is useful when (de)serializing data from and to binary representations. For example, Protocol Buffers (by Google) use these kinds of encoding.
use integer_encoding::*;
fn main() {
let a: u32 = 344;
let encoded_byte_slice = a.encode_fixed_light();
assert_eq!(Some(a), u32::decode_fixed(&encoded_byte_slice));
assert_eq!(4, encoded_byte_slice.len());
let b: i32 = -111;
let encoded_byte_vec = b.encode_var_vec();
assert_eq!(Some((b, 2)), i32::decode_var(&encoded_byte_vec));
}
Traitsยง
- Fixed
Int FixedInt
provides encoding/decoding to and from fixed int representations. Note that current Rust versions already provide this functionality via theto_le_bytes()
andto_be_bytes()
methods.- Fixed
IntAsync Reader - Like FixedIntReader, but returns a future.
- Fixed
IntAsync Writer - Fixed
IntReader - A trait for reading FixedInts from any other
Reader
. - Fixed
IntWriter - A trait for writing integers without encoding (i.e.
FixedInt
) to anyWrite
type. - VarInt
- Varint (variable length integer) encoding, as described in https://developers.google.com/protocol-buffers/docs/encoding.
- VarInt
Async Reader - Like a VarIntReader, but returns a future.
- VarInt
Async Writer - Like VarIntWriter, but asynchronous.
- VarInt
Reader - A trait for reading VarInts from any other
Reader
. - VarInt
Writer - A trait for writing integers in VarInt encoding to any
Write
type. This packs encoding and writing into one step.