pub struct BitReader<R: Read> { /* private fields */ }Implementationsยง
Trait Implementationsยง
Sourceยงimpl<R: Read> Read for BitReader<R>
impl<R: Read> Read for BitReader<R>
Sourceยงfn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Reads bytes from the underlying bit stream.
This method behaves differently depending on the bit buffer state:
- When the bit buffer is empty (byte-aligned state), it delegates directly to the inner reader
- When the bit buffer contains unconsumed bits (non-byte-aligned state), it returns an
UnalignedAccesserror
ยงByte Alignment Requirement
The fundamental reason for this behavior is bit stream integrity. When partially consumed bits exist in the buffer:
- Direct byte access would bypass the bit bufferโs state tracking
- Reading bytes would consume underlying bytes that contain the remaining portions of partially read bit sequences
- This would irreversibly corrupt the bit-level parsing state
ยงCorrect Usage
To read byte data:
- Use
is_byte_aligned()to check if youโre in byte-aligned state - For mixed bit/byte reading, always consume all bits in the current byte before reading bytes
ยงErrors
Returns BitReadWriteError::UnalignedAccess (wrapped in io::Error) when called with
non-empty bit buffer. This prevents:
- Undefined state transitions
- Silent data corruption
- Loss of partially buffered bits
1.36.0 ยท Sourceยงfn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like
read, except that it reads into a slice of buffers. Read moreSourceยงfn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
๐ฌThis is a nightly-only experimental API. (
can_vector)1.0.0 ยท Sourceยงfn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Reads all bytes until EOF in this source, placing them into
buf. Read more1.0.0 ยท Sourceยงfn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Reads all bytes until EOF in this source, appending them to
buf. Read more1.6.0 ยท Sourceยงfn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Reads the exact number of bytes required to fill
buf. Read moreSourceยงfn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
๐ฌThis is a nightly-only experimental API. (
read_buf)Pull some bytes from this source into the specified buffer. Read more
Sourceยงfn read_buf_exact(
&mut self,
cursor: BorrowedCursor<'_, u8>,
) -> Result<(), Error>
fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>
๐ฌThis is a nightly-only experimental API. (
read_buf)Reads the exact number of bytes required to fill
cursor. Read more1.0.0 ยท Sourceยงfn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Creates a โby referenceโ adapter for this instance of
Read. Read more1.0.0 ยท Sourceยงfn chain<R>(self, next: R) -> Chain<Self, R> โ
fn chain<R>(self, next: R) -> Chain<Self, R> โ
Creates an adapter which will chain this stream with another. Read more
1.0.0 ยท Sourceยงfn take(self, limit: u64) -> Take<Self> โwhere
Self: Sized,
fn take(self, limit: u64) -> Take<Self> โwhere
Self: Sized,
Creates an adapter which will read at most
limit bytes from it. Read moreSourceยงfn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
๐ฌThis is a nightly-only experimental API. (
read_array)Read and return a fixed array of bytes from this source. Read more
Auto Trait Implementationsยง
impl<R> Freeze for BitReader<R>where
R: Freeze,
impl<R> RefUnwindSafe for BitReader<R>where
R: RefUnwindSafe,
impl<R> Send for BitReader<R>where
R: Send,
impl<R> Sync for BitReader<R>where
R: Sync,
impl<R> Unpin for BitReader<R>where
R: Unpin,
impl<R> UnsafeUnpin for BitReader<R>where
R: UnsafeUnpin,
impl<R> UnwindSafe for BitReader<R>where
R: UnwindSafe,
Blanket Implementationsยง
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