Crates.io | leben |
lib.rs | leben |
version | 0.1.0 |
source | src |
created_at | 2018-09-26 12:31:52.131955 |
updated_at | 2018-09-26 12:31:52.131955 |
description | LEB128 Encoder / Decoder |
homepage | |
repository | https://github.com/npmccallum/leben |
max_upload_size | |
id | 86627 |
size | 29,269 |
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
)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[..]);