Expand description
A minimal library to read and write integers encoded using LEB128.
Unlike other LEB128 libraries there are three notable changes:
- Uses
zc_ioinstead of the standard library forno_stdcompatability. The standard library can be used throughzc_io::IoReaderandzc_io::IoWriterinstances. - 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 tozc_io). - 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.