nt_leb128/lib.rs
1//! Read and write DWARF's "Little Endian Base 128" (LEB128) variable length
2//! integer encoding.
3//!
4//! The implementation is a direct translation of the psuedocode in the DWARF 4
5//! standard's appendix C.
6//!
7//! Read and write signed integers:
8//!
9//! ```
10//! use leb128::write::LEB128Write;
11//! use leb128::read::LEB128Read;
12//!
13//! let mut buf = [0; 1024];
14//!
15//! // Write to anything that implements `std::io::Write`.
16//! {
17//! let mut writable = &mut buf[..];
18//! writable.write_signed(-12345).expect("Should write number");
19//! }
20//!
21//! // Read from anything that implements `std::io::Read`.
22//! let mut readable = &buf[..];
23//! let val = readable.read_signed().expect("Should read number");
24//! assert_eq!(val, -12345);
25//! ```
26//!
27//! Or read and write unsigned integers:
28//!
29//! ```
30//! use leb128::write::LEB128Write;
31//! use leb128::read::LEB128Read;
32//!
33//! let mut buf = [0; 1024];
34//!
35//! {
36//! let mut writable = &mut buf[..];
37//! writable.write_unsigned(98765).expect("Should write number");
38//! }
39//!
40//! let mut readable = &buf[..];
41//! let val = readable.read_signed().expect("Should read number");
42//! assert_eq!(val, 98765);
43//! ```
44
45#![deny(missing_docs)]
46
47#[doc(hidden)]
48pub const CONTINUATION_BIT: u8 = 1 << 7;
49#[doc(hidden)]
50pub const SIGN_BIT: u8 = 1 << 6;
51
52#[doc(hidden)]
53#[inline]
54pub fn low_bits_of_byte(byte: u8) -> u8 {
55 byte & !CONTINUATION_BIT
56}
57
58#[doc(hidden)]
59#[inline]
60pub fn low_bits_of_u64(val: u64) -> u8 {
61 let byte = val & (std::u8::MAX as u64);
62 low_bits_of_byte(byte as u8)
63}
64
65/// A module for reading signed and unsigned integers that have been LEB128
66/// encoded.
67pub mod read;
68
69/// A module for writing integers encoded as LEB128.
70pub mod write;
71
72pub use self::read::LEB128Read;
73pub use self::write::LEB128Write;
74
75#[cfg(test)]
76mod tests_bytes;
77