Struct BitCursor

Source
pub struct BitCursor<T: AsRef<[u8]>> { /* private fields */ }
Expand description

A no-copy cursor wrapper for a bitstream.

Any type that implements AsRef<[u8]> can be used with BitCursor.

Implementations§

Source§

impl<T: AsRef<[u8]>> BitCursor<T>

Source

pub fn new(inner: T) -> Self

Create a new BitCursor for the inner buffer.

Source

pub fn new_with_len(inner: T, byte_len: usize) -> Result<Self, Error>

Create a new BitCursor for the inner buffer, limiting to byte_len bytes.

Returns an error if byte_len exceeds inner’s range.

Source

pub fn byte_len(&self) -> usize

Return the length of the data wrapped by this cursor, in bytes.

Source

pub fn bit_len(&self) -> usize

Return the length of the data wrapped by this cursor, in bits.

Source

pub fn tell_bit(&self) -> usize

Return the current position in the data, at bit granularity.

Source

pub fn tell_byte(&self) -> usize

Return the current position in the data, at byte granularity.

Source

pub fn exhausted(&self) -> bool

Return whether the underlying data is “exhausted”, i.e. whether it’s impossible to read any further from the cursor’s current position.

Source

pub fn seek_bit(&mut self, pos: usize) -> Result<(), Error>

Seek to the given bit-granular position in the bitstream.

NOTE: This is a bit-granular absolute seek. If you only need byte granularity or would like to do a relative (start or end) seek, use the Seek implementation.

Source

pub fn read(&mut self, nbits: usize) -> Result<u64, Error>

Read nbits bits of data at the current position. The data is returned as a u64.

Returns an error if the requested read is invalid (e.g. EOF or not enough data) or if nbits is invalid (zero, or >= u64::BITS).

Source

pub fn read_as<Int: NumCast>(&mut self, nbits: usize) -> Result<Int, Error>

Read a nbits of data at the current position into the given scalar type.

Returns an error under all of the same conditions as read, as well as if the read value doesn’t fit into the given scalar.

Source

pub fn read_exact<Int: NumCast>(&mut self) -> Result<Int, Error>

Read exactly the size of Int at the current position.

Returns an error under all of the same conditions as read.

Source

pub fn read_vbr(&mut self, width: usize) -> Result<u64, Error>

Read a width-wide VBR-encoded integer.

This function returns only unsigned integers. For signed integers, use read_svbr.

Source

pub fn read_svbr(&mut self, width: usize) -> Result<isize, Error>

Return a width-side signed VBR-encoded integer from cursor.

This function returns only signed integers, assuming LLVM’s signed VBR representation.

Source

pub fn align32(&mut self)

Align the stream on the next 32-bit boundary.

Any data consumed during alignment is discarded.

Trait Implementations§

Source§

impl<T: Debug + AsRef<[u8]>> Debug for BitCursor<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: AsRef<[u8]>> Seek for BitCursor<T>

A Seek implementation for BitCursor.

Seeking past the end of a BitCursor is always invalid, and always returns an error.

NOTE: This is a byte-granular implementation of Seek. For bit-granular seeking, use seek_bit.

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
Source§

fn stream_position(&mut self) -> Result<u64>

Returns the current seek position from the start of the stream. Read more
1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BitCursor<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BitCursor<T>
where T: RefUnwindSafe,

§

impl<T> Send for BitCursor<T>
where T: Send,

§

impl<T> Sync for BitCursor<T>
where T: Sync,

§

impl<T> Unpin for BitCursor<T>
where T: Unpin,

§

impl<T> UnwindSafe for BitCursor<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.