pub struct BitReader<R, E: Endianness> { /* private fields */ }
Expand description
For reading non-aligned bits from a stream of bytes in a given endianness.
This will read exactly as many whole bytes needed to return the requested number of bits. It may cache up to a single partial byte but no more.
Implementations§
Source§impl<R, E: Endianness> BitReader<R, E>
impl<R, E: Endianness> BitReader<R, E>
Source§impl<R: Read, E: Endianness> BitReader<R, E>
impl<R: Read, E: Endianness> BitReader<R, E>
Sourcepub fn reader(&mut self) -> Option<&mut R>
pub fn reader(&mut self) -> Option<&mut R>
If stream is byte-aligned, provides mutable reference
to internal reader. Otherwise returns None
Sourcepub fn aligned_reader(&mut self) -> &mut R
pub fn aligned_reader(&mut self) -> &mut R
Returns byte-aligned mutable reference to internal reader.
Bytes aligns stream if it is not already aligned.
Sourcepub fn into_bytereader(self) -> ByteReader<R, E>
pub fn into_bytereader(self) -> ByteReader<R, E>
Converts BitReader
to ByteReader
in the same endianness.
§Warning
Any unread partial bits are discarded.
Sourcepub fn bytereader(&mut self) -> Option<ByteReader<&mut R, E>>
pub fn bytereader(&mut self) -> Option<ByteReader<&mut R, E>>
If stream is byte-aligned, provides temporary ByteReader
in the same endianness. Otherwise returns None
§Warning
Any reader bits left over when ByteReader
is dropped are lost.
Source§impl<R, E> BitReader<R, E>
impl<R, E> BitReader<R, E>
Sourcepub fn seek_bits(&mut self, from: SeekFrom) -> Result<u64>
pub fn seek_bits(&mut self, from: SeekFrom) -> Result<u64>
§Example
use std::io::{Read, Cursor, SeekFrom};
use bitstream_io::{BigEndian, BitReader, BitRead};
let data = [0x00, 0xFF];
let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);
assert_eq!(reader.position_in_bits().unwrap(), 0);
let pos = reader.seek_bits(SeekFrom::Start(5)).unwrap();
assert!(pos == 5 && 5 == reader.position_in_bits().unwrap());
let pos = reader.seek_bits(SeekFrom::Current(-2)).unwrap();
assert!(pos == 3 && 3 == reader.position_in_bits().unwrap());
let pos = reader.seek_bits(SeekFrom::End(5)).unwrap();
assert!(pos == 11 && 11 == reader.position_in_bits().unwrap());
Sourcepub fn position_in_bits(&mut self) -> Result<u64>
pub fn position_in_bits(&mut self) -> Result<u64>
§Example
use std::fs::read;
use std::io::{Read, Cursor, SeekFrom};
use bitstream_io::{BigEndian, BitReader, BitRead};
let data = [0x00, 0xFF];
let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);
assert_eq!(reader.position_in_bits().unwrap(), 0);
let _: i32 = reader.read_signed::<5, _>().unwrap();
assert_eq!(reader.position_in_bits().unwrap(), 5);
reader.read_bit().unwrap();
assert_eq!(reader.position_in_bits().unwrap(), 6);
Trait Implementations§
Source§impl<R: Read, E: Endianness> BitRead for BitReader<R, E>
impl<R: Read, E: Endianness> BitRead for BitReader<R, E>
Source§fn skip(&mut self, bits: u32) -> Result<()>
fn skip(&mut self, bits: u32) -> Result<()>
§Examples
use std::io::Read;
use bitstream_io::{BigEndian, BitReader, BitRead};
let data = [0b10110111];
let mut reader = BitReader::endian(data.as_slice(), BigEndian);
assert!(reader.skip(3).is_ok());
assert_eq!(reader.read::<5, u8>().unwrap(), 0b10111);
use std::io::Read;
use bitstream_io::{LittleEndian, BitReader, BitRead};
let data = [0b10110111];
let mut reader = BitReader::endian(data.as_slice(), LittleEndian);
assert!(reader.skip(3).is_ok());
assert_eq!(reader.read::<5, u8>().unwrap(), 0b10110);
Source§fn read_bytes(&mut self, buf: &mut [u8]) -> Result<()>
fn read_bytes(&mut self, buf: &mut [u8]) -> Result<()>
§Example
use std::io::Read;
use bitstream_io::{BigEndian, BitReader, BitRead};
let data = b"foobar";
let mut reader = BitReader::endian(data.as_slice(), BigEndian);
assert!(reader.skip(24).is_ok());
let mut buf = [0;3];
assert!(reader.read_bytes(&mut buf).is_ok());
assert_eq!(&buf, b"bar");
Source§fn read_unsigned_counted<const BITS: u32, U>(
&mut self,
bits: BitCount<BITS>,
) -> Result<U>where
U: UnsignedInteger,
fn read_unsigned_counted<const BITS: u32, U>(
&mut self,
bits: BitCount<BITS>,
) -> Result<U>where
U: UnsignedInteger,
Reads an unsigned value from the stream with
the given number of bits. Read more
Source§fn read_unsigned<const BITS: u32, U>(&mut self) -> Result<U>where
U: UnsignedInteger,
fn read_unsigned<const BITS: u32, U>(&mut self) -> Result<U>where
U: UnsignedInteger,
Reads an unsigned value from the stream with
the given constant number of bits. Read more
Source§fn read_signed_counted<const MAX: u32, S>(
&mut self,
bits: impl TryInto<SignedBitCount<MAX>>,
) -> Result<S>where
S: SignedInteger,
fn read_signed_counted<const MAX: u32, S>(
&mut self,
bits: impl TryInto<SignedBitCount<MAX>>,
) -> Result<S>where
S: SignedInteger,
Reads a twos-complement signed value from the stream with
the given number of bits. Read more
Source§fn read_signed<const BITS: u32, S>(&mut self) -> Result<S>where
S: SignedInteger,
fn read_signed<const BITS: u32, S>(&mut self) -> Result<S>where
S: SignedInteger,
Reads a twos-complement signed value from the stream with
the given constant number of bits. Read more
Source§fn read_to<V>(&mut self) -> Result<V>where
V: Primitive,
fn read_to<V>(&mut self) -> Result<V>where
V: Primitive,
Reads whole value from the stream whose size in bits is equal
to its type’s size. Read more
Source§fn read_as_to<F, V>(&mut self) -> Result<V>where
F: Endianness,
V: Primitive,
fn read_as_to<F, V>(&mut self) -> Result<V>where
F: Endianness,
V: Primitive,
Reads whole value from the stream whose size in bits is equal
to its type’s size in an endianness that may be different
from the stream’s endianness. Read more
Source§fn read_unary<const STOP_BIT: u8>(&mut self) -> Result<u32>
fn read_unary<const STOP_BIT: u8>(&mut self) -> Result<u32>
Counts the number of bits in the stream until
STOP_BIT
and returns the amount read.
STOP_BIT
must be 0 or 1.
Because this field is variably-sized and may be large,
its output is always a u32
type. Read moreSource§fn byte_aligned(&self) -> bool
fn byte_aligned(&self) -> bool
Returns true if the stream is aligned at a whole byte. Read more
Source§fn byte_align(&mut self)
fn byte_align(&mut self)
Throws away all unread bit values until the next whole byte.
Does nothing if the stream is already aligned. Read more
Source§fn read<const BITS: u32, I>(&mut self) -> Result<I>where
I: Integer,
fn read<const BITS: u32, I>(&mut self) -> Result<I>where
I: Integer,
Reads a signed or unsigned value from the stream with
the given constant number of bits. Read more
Source§fn read_var<I>(&mut self, bits: u32) -> Result<I>
fn read_var<I>(&mut self, bits: u32) -> Result<I>
Reads a signed or unsigned value from the stream with
the given variable number of bits. Read more
Source§fn read_count<const MAX: u32>(&mut self) -> Result<BitCount<MAX>>
fn read_count<const MAX: u32>(&mut self) -> Result<BitCount<MAX>>
Given a desired maximum value for a bit count,
reads the necessary bits to fill up to that amount. Read more
Source§fn read_counted<const MAX: u32, I>(&mut self, bits: BitCount<MAX>) -> Result<I>
fn read_counted<const MAX: u32, I>(&mut self, bits: BitCount<MAX>) -> Result<I>
Reads a signed or unsigned value from the stream with
the given number of bits. Read more
Source§fn read_unsigned_var<U>(&mut self, bits: u32) -> Result<U>where
U: UnsignedInteger,
fn read_unsigned_var<U>(&mut self, bits: u32) -> Result<U>where
U: UnsignedInteger,
Reads an unsigned value from the stream with
the given number of bits. Read more
Source§fn read_signed_var<S>(&mut self, bits: u32) -> Result<S>where
S: SignedInteger,
fn read_signed_var<S>(&mut self, bits: u32) -> Result<S>where
S: SignedInteger,
Reads a twos-complement signed value from the stream with
the given number of bits. Read more
Source§fn read_const<const BITS: u32, const VALUE: u32, E>(
&mut self,
err: E,
) -> Result<(), E>
fn read_const<const BITS: u32, const VALUE: u32, E>( &mut self, err: E, ) -> Result<(), E>
Reads the given constant value from the stream with the
given number of bits. Read more
Source§fn read_to_bytes<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]>
fn read_to_bytes<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]>
👎Deprecated since 1.8.0: use read_to() method instead
Completely fills a whole buffer with bytes and returns it.
If the stream is already byte-aligned, it will map
to a faster
read_exact
call. Otherwise it will read
bytes individually in 8-bit increments. Read moreSource§fn read_to_vec(&mut self, bytes: usize) -> Result<Vec<u8>>
fn read_to_vec(&mut self, bytes: usize) -> Result<Vec<u8>>
Completely fills a vector of bytes and returns it.
If the stream is already byte-aligned, it will map
to a faster
read_exact
call. Otherwise it will read
bytes individually in 8-bit increments. Read moreSource§fn parse_with<'a, F: FromBitStreamWith<'a>>(
&mut self,
context: &F::Context,
) -> Result<F, F::Error>
fn parse_with<'a, F: FromBitStreamWith<'a>>( &mut self, context: &F::Context, ) -> Result<F, F::Error>
Parses and returns complex type with context
Auto Trait Implementations§
impl<R, E> Freeze for BitReader<R, E>where
R: Freeze,
impl<R, E> RefUnwindSafe for BitReader<R, E>where
R: RefUnwindSafe,
E: RefUnwindSafe,
impl<R, E> Send for BitReader<R, E>
impl<R, E> Sync for BitReader<R, E>
impl<R, E> Unpin for BitReader<R, E>
impl<R, E> UnwindSafe for BitReader<R, E>where
R: UnwindSafe,
E: UnwindSafe,
Blanket Implementations§
Source§impl<R> BitRead2 for Rwhere
R: BitRead,
impl<R> BitRead2 for Rwhere
R: BitRead,
Source§fn read<I>(&mut self, bits: u32) -> Result<I, Error>where
I: Integer,
fn read<I>(&mut self, bits: u32) -> Result<I, Error>where
I: Integer,
Reads an unsigned value from the stream with
the given number of bits. Read more
Source§fn read_in<const BITS: u32, I>(&mut self) -> Result<I, Error>where
I: Integer,
fn read_in<const BITS: u32, I>(&mut self) -> Result<I, Error>where
I: Integer,
Reads an unsigned value from the stream with
the given constant number of bits. Read more
Source§fn read_signed<S>(&mut self, bits: u32) -> Result<S, Error>where
S: SignedInteger,
fn read_signed<S>(&mut self, bits: u32) -> Result<S, Error>where
S: SignedInteger,
Reads a twos-complement signed value from the stream with
the given number of bits. Read more
Source§fn read_signed_in<const BITS: u32, S>(&mut self) -> Result<S, Error>where
S: SignedInteger,
fn read_signed_in<const BITS: u32, S>(&mut self) -> Result<S, Error>where
S: SignedInteger,
Reads a twos-complement signed value from the stream with
the given constant number of bits. Read more
Source§fn read_to<V>(&mut self) -> Result<V, Error>where
V: Primitive,
fn read_to<V>(&mut self) -> Result<V, Error>where
V: Primitive,
Reads whole value from the stream whose size in bits is equal
to its type’s size. Read more
Source§fn read_as_to<F, V>(&mut self) -> Result<V, Error>where
F: Endianness,
V: Primitive,
fn read_as_to<F, V>(&mut self) -> Result<V, Error>where
F: Endianness,
V: Primitive,
Reads whole value from the stream whose size in bits is equal
to its type’s size in an endianness that may be different
from the stream’s endianness. Read more
Source§fn skip(&mut self, bits: u32) -> Result<(), Error>
fn skip(&mut self, bits: u32) -> Result<(), Error>
Skips the given number of bits in the stream.
Since this method does not need an accumulator,
it may be slightly faster than reading to an empty variable.
In addition, since there is no accumulator,
there is no upper limit on the number of bits
which may be skipped.
These bits are still read from the stream, however,
and are never skipped via a
seek
method. Read moreSource§fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error>
Completely fills the given buffer with whole bytes.
If the stream is already byte-aligned, it will map
to a faster
read_exact
call. Otherwise it will read
bytes individually in 8-bit increments. Read moreSource§fn read_unary0(&mut self) -> Result<u32, Error>
fn read_unary0(&mut self) -> Result<u32, Error>
Counts the number of 1 bits in the stream until the next
0 bit and returns the amount read.
Because this field is variably-sized and may be large,
its output is always a
u32
type. Read moreSource§fn read_unary1(&mut self) -> Result<u32, Error>
fn read_unary1(&mut self) -> Result<u32, Error>
Counts the number of 0 bits in the stream until the next
1 bit and returns the amount read.
Because this field is variably-sized and may be large,
its output is always a
u32
type. Read moreSource§fn byte_aligned(&self) -> bool
fn byte_aligned(&self) -> bool
Returns true if the stream is aligned at a whole byte.
Source§fn byte_align(&mut self)
fn byte_align(&mut self)
Throws away all unread bit values until the next whole byte.
Does nothing if the stream is already aligned.
Source§fn read_to_bytes<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]>
fn read_to_bytes<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]>
👎Deprecated since 1.8.0: use read_to() method instead
Completely fills a whole buffer with bytes and returns it.
If the stream is already byte-aligned, it will map
to a faster
read_exact
call. Otherwise it will read
bytes individually in 8-bit increments. Read moreSource§fn read_to_vec(&mut self, bytes: usize) -> Result<Vec<u8>>
fn read_to_vec(&mut self, bytes: usize) -> Result<Vec<u8>>
Completely fills a vector of bytes and returns it.
If the stream is already byte-aligned, it will map
to a faster
read_exact
call. Otherwise it will read
bytes individually in 8-bit increments. Read moreSource§fn parse<F: FromBitStream>(&mut self) -> Result<F, F::Error>where
Self: BitRead,
fn parse<F: FromBitStream>(&mut self) -> Result<F, F::Error>where
Self: BitRead,
Parses and returns complex type
Source§fn parse_with<'a, F: FromBitStreamWith<'a>>(
&mut self,
context: &F::Context,
) -> Result<F, F::Error>where
Self: BitRead,
fn parse_with<'a, F: FromBitStreamWith<'a>>(
&mut self,
context: &F::Context,
) -> Result<F, F::Error>where
Self: BitRead,
Parses and returns complex type with context
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more