4 releases
0.2.0 | Apr 2, 2019 |
---|---|
0.1.2 | Feb 8, 2016 |
0.1.1 | Dec 31, 2015 |
0.1.0 | Dec 28, 2015 |
#89 in #reader-writer
676 downloads per month
Used in 3 crates
15KB
312 lines
bitbit
Bit-at-a-time reader/writer types for Rust
Credits and license
Copyright (c) 2015 Robert Norris. MIT license. See LICENSE.
lib.rs
:
bitbit provides functions to read and write streams one bit at a time.
Reading
let r = File::open("somefile")?;
let mut br = BitReader::new(r);
let is_one = br.read_bit()?;
let byte = br.read_byte()?;
let num = br.read_bits(5)?;
Using a buffered reader will improve performance:
let r = File::open("somefile")?;
let buff_reader = BufReader::new(r);
let mut br: BitReader<_, MSB> = BitReader::new(buff_reader);
Writing
let w = File::create("somefile")?;
let mut bw = BitWriter::new(w);
bw.write_bit(true)?;
bw.write_byte(0x55)?;
bw.write_bits(0x15, 5)?;
bw.pad_to_byte();
Using a buffered writer will improve performance
let w = File::create("somefile")?;
let mut buf_writer = BufWriter::new(w);
let mut bw = BitWriter::new(&mut buf_writer);
...
buf_writer.flush();