Crate mini_leb128

Crate mini_leb128 

Source
Expand description

A minimal library to read and write integers encoded using LEB128.

Unlike other LEB128 libraries there are three notable changes:

  1. Uses zc_io instead of the standard library for no_std compatability. The standard library can be used through zc_io::IoReader and zc_io::IoWriter instances.
  2. When writing encoded integers, an internal buffer on the stack is used to possibly reduce system calls; each encoded integer makes a single call to write_all. This is particularly useful since buffered writers are frequently underutilized (and not native to zc_io).
  3. Methods always return how many bytes were used when reading or writing the integers, which may help in instances where that information would have to get retrospectively computed.

If none of these changes are meaningful to you, consider another LEB128 project, as they would have less friction when just using the standard library’s I/O interfaces.

§Examples

Read and write unsigned integers:

let mut buf = [0; 5];

let encoded_length = mini_leb128::write_u32(buf.as_mut_slice(), 624_485)?;
assert_eq!(encoded_length.get(), 3);
assert_eq!(buf, [0xE5, 0x8E, 0x26, 0x00, 0x00]);

let (value, bytes_read) = mini_leb128::read_u32(buf.as_slice())?;
assert_eq!(value, 624_485);
assert_eq!(bytes_read.get(), 3);

Read and write signed integers:

let mut buf = [0; 5];

let encoded_length = mini_leb128::write_i32(buf.as_mut_slice(), -123_456)?;
assert_eq!(encoded_length.get(), 3);
assert_eq!(buf, [0xC0, 0xBB, 0x78, 0x00, 0x00]);

let (value, bytes_read) = mini_leb128::read_i32(buf.as_slice())?;
assert_eq!(value, -123_456);
assert_eq!(bytes_read.get(), 3);

Functions§

read_i32
Decodes a signed 32-bit integer using LEB128.
read_i64
Decodes a signed 64-bit integer using LEB128.
read_i128
Decodes a signed 128-bit integer using LEB128.
read_u32
Decodes an unsigned 32-bit integer using LEB128.
read_u64
Decodes an unsigned 64-bit integer using LEB128.
read_u128
Decodes an unsigned 128-bit integer using LEB128.
write_i32
Encodes a signed 32-bit integer using LEB128.
write_i64
Encodes a signed 64-bit integer using LEB128.
write_i128
Encodes a signed 128-bit integer using LEB128.
write_u32
Encodes an unsigned 32-bit integer using LEB128.
write_u64
Encodes an unsigned 64-bit integer using LEB128.
write_u128
Encodes an unsigned 128-bit integer using LEB128.