Struct bitreader::BitReader [] [src]

pub struct BitReader<'a> { /* fields omitted */ }

BitReader reads data from a byte slice at the granularity of a single bit.

Methods

impl<'a> BitReader<'a>
[src]

Construct a new BitReader from a byte slice. The returned reader lives at most as long as the slice given to is valid.

Returns a copy of current BitReader, with the difference that its position() returns positions relative to the position of the original BitReader at the construction time. After construction, both readers are otherwise completely independent, except of course for sharing the same source data.

use bitreader::BitReader;

let bytes = &[0b11110000, 0b00001111];
let mut original = BitReader::new(bytes);
assert_eq!(original.read_u8(4).unwrap(), 0b1111);
assert_eq!(original.position(), 4);

let mut relative = original.relative_reader();
assert_eq!(relative.position(), 0);

assert_eq!(original.read_u8(8).unwrap(), 0);
assert_eq!(relative.read_u8(8).unwrap(), 0);

assert_eq!(original.position(), 12);
assert_eq!(relative.position(), 8);

Read at most 8 bits into a u8.

Read at most 16 bits into a u16.

Read at most 32 bits into a u32.

Read at most 64 bits into a u64.

Read at most 8 bits into a i8. Assumes the bits are stored in two's complement format.

Read at most 16 bits into a i16. Assumes the bits are stored in two's complement format.

Read at most 32 bits into a i32. Assumes the bits are stored in two's complement format.

Read at most 64 bits into a i64. Assumes the bits are stored in two's complement format.

Read a single bit as a boolean value. Interprets 1 as true and 0 as false.

Skip arbitrary number of bits. However, you can skip at most to the end of the byte slice.

Returns the position of the cursor, or how many bits have been read so far.

Helper to make sure the "bit cursor" is exactly at the beginning of a byte, or at specific multi-byte alignment position.

For example reader.is_aligned(1) returns true if exactly n bytes, or n * 8 bits, has been read. Similarly, reader.is_aligned(4) returns true if exactly n * 32 bits, or n 4-byte sequences has been read.

This function can be used to validate the data is being read properly, for example by adding invocations wrapped into debug_assert!() to places where it is known the data should be n-byte aligned.