[−][src]Trait bitcursor::ReadBits
The 'ReadBits' trait allows reading bits of size unit (bool, u8, u32, etc.), at the given bit/cursor position
Implementors of 'ReadBits' are defined by one required method, ['read_bits']. Each call to ['read_bits'] will attempt to read bits of unit size from the source, given the source has enough bits for the unit size, otherwise it returns an Error value
Note: If the intended use is to grab each byte (size u8), Using an implementor of 'BufRead' will be more efficient.
Examples
Read a u128 from a list of u8's first from bit position 0, and next and bit position 2, and cursor position 1 (8+2 = 10)
use {BitCursor, ReadBits}; use std::io::Seek; use std::io::SeekFrom; fn main() -> std::io::Result<()> { let data: [u8; 22] = [ 0b01101010, 0b11110001, 0b01110100, 0b10100001, 0b11100011, 0b11000000, 0b11110001, 0b01110100, 0b10100001, 0b11100011, 0b11000000, 0b01101010, 0b11110001, 0b01110100, 0b10100001, 0b11100011, 0b11000000, 0b11110001, 0b01110100, 0b10100001, 0b11100011, 0b11000000, ]; let mut bcurs = BitCursor::new(&data[..]); let r = bcurs.read_bits::<u128>().unwrap(); assert_eq!( 0b01101010111100010111010010100001111000111100000011110001011101001010000111100011110000000110101011110001011101001010000111100011 as u128, r ); let _ = bcurs.seek(SeekFrom::Start(10)); let r = bcurs.read_bits::<u128>().unwrap(); assert_eq!( 0b11000101110100101000011110001111000000111100010111010010100001111000111100000001101010111100010111010010100001111000111100000011 as u128, r ); Ok(()) }
Read a u8 from a list of u32's
use {BitCursor, ReadBits}; use std::io::Seek; use std::io::SeekFrom; fn main() -> std::io::Result<()> { let data: [u32; 3] = [ 0b10010010001001011000100001101010, 0b10010010001001011000100001101010, 0b10010010001001011000100001101010, ]; let mut bcurs = BitCursor::new(&data[..]); let r = bcurs.read_bits::<u8>().unwrap(); assert_eq!(0b10010010 as u8, r); let _ = bcurs.seek(SeekFrom::Start(10)); let r = bcurs.read_bits::<u8>().unwrap(); assert_eq!(0b10010110 as u8, r); }
Required methods
fn read_bits<U: Unit>(&mut self) -> Result<U>
Read the bits of size <T: Unit> starting from the bit position of the cursor
Example
use {BitCursor, ReadBits}; use std::io::Seek; use std::io::SeekFrom; fn main() -> std::io::Result<()> { let data: [u32; 3] = [ 0b10010010001001011000100001101010, 0b10010010001001011000100001101010, 0b10010010001001011000100001101010, ]; let mut bcurs = BitCursor::new(&data[..]); let r = bcurs.read_bits::<u8>().unwrap(); assert_eq!(0b10010010 as u8, r); let _ = bcurs.seek(SeekFrom::Start(10)); let r = bcurs.read_bits::<u8>().unwrap(); assert_eq!(0b10010110 as u8, r); }