Uses old Rust 2015
0.1.0 |
|
---|
#17 in #leb128
18KB
327 lines
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[..]);