leben

Crates.ioleben
lib.rsleben
version0.1.0
sourcesrc
created_at2018-09-26 12:31:52.131955
updated_at2018-09-26 12:31:52.131955
descriptionLEB128 Encoder / Decoder
homepage
repositoryhttps://github.com/npmccallum/leben
max_upload_size
id86627
size29,269
Nathaniel McCallum (npmccallum)

documentation

https://docs.rs/leben

README

Build Status Rust Version 1.28+ Crate Docs

Leben is a crate for encoding or decoding integers in LEB128 format.

This is accomplished by extending the Rust native integer types with two traits:

  • Reader (one associated function: leb128_read)
  • Writer (one method: leb128_write)

Examples

Reading and writing is done on any value that implements the std::io::Read or std::io::Write, respectively. For example, we can write to std::io::Sink:

use std::io::sink;
use leben::Writer;

let mut writer = sink();
let number: i16 = -582;
number.leb128_write(&mut writer).unwrap();

Don't forget that std::vec::Vec<u8> implements std::io::Write and [u8] implements std::io::Read:

use leben::{Reader, Writer};
use std::io::Write;

let encoded = [198, 253, 255, 127];
let decoded = 268435142u64;

let value = u64::leb128_read(&mut &encoded[..]).unwrap();
assert_eq!(value, decoded);

let mut value: Vec<u8> = Vec::new();
decoded.leb128_write(&mut value).unwrap();
assert_eq!(&value[..], &encoded[..]);
Commit count: 27

cargo fmt