#leb128 #variables #length

no-std nano-leb128

Little endian base 128 variable-length code compression

1 unstable release

0.1.0 Jul 7, 2019

#350 in Compression

Download history 310/week @ 2023-12-06 93/week @ 2023-12-13 34/week @ 2023-12-20 8/week @ 2023-12-27 343/week @ 2024-01-03 276/week @ 2024-01-10 168/week @ 2024-01-17 170/week @ 2024-01-24 85/week @ 2024-01-31 212/week @ 2024-02-07 81/week @ 2024-02-14 189/week @ 2024-02-21 210/week @ 2024-02-28 290/week @ 2024-03-06 299/week @ 2024-03-13 203/week @ 2024-03-20

1,036 downloads per month
Used in 4 crates

MIT/Apache

20KB
252 lines

nano: leb128

Little endian base 128 variable-length code compression.

Usage

Signed LEB128 compression/decompression:

use nano_leb128::SLEB128;

fn rand_i64() -> i64 {
    // ...
}

let mut buf = [0; 10];
let value = rand_i64();

// Compress the value into the buffer.
let len = SLEB128::from(value).write_into(&mut buf).unwrap();

// Decompress the value from the buffer.
let (decompressed, _len) = SLEB128::read_from(&buf[..len]).unwrap();

assert_eq!(i64::from(decompressed), value);

Unsigned LEB128 compression/decompression:

use nano_leb128::ULEB128;

fn rand_u64() -> u64 {
    // ...
}

let mut buf = [0; 10];
let value = rand_u64();

// Compress the value into the buffer.
let len = ULEB128::from(value).write_into(&mut buf).unwrap();

// Decompress the value from the buffer.
let (decompressed, _len) = ULEB128::read_from(&buf[..len]).unwrap();

assert_eq!(u64::from(decompressed), value);

Features

  • std (enabled by default)

    This enables extensions that are only available with the Rust standard library.

  • std_io_ext

    Adds methods for reading/writing LEB128 compressed values from implementors of the traits in std::io. This feature requires the std feature and will automatically enable it if it is not already enabled.

  • byteio_ext

    Adds methods for reading/writing LEB128 compressed values from implementors of the traits in byteio. This feature does not require the std feature.

License

This project is dual-licensed under either of

at your option.

Dependencies

~48KB