Skip to main content

BitCursor

Struct BitCursor 

Source
pub struct BitCursor<'a> { /* private fields */ }
Expand description

A big-endian, MSB-first bit reader.

Implementations§

Source§

impl<'a> BitCursor<'a>

Source

pub const fn new(data: &'a [u8]) -> Self

Creates a cursor at bit 0 of data.

Source

pub const fn data(&self) -> &'a [u8] ⓘ

The underlying buffer.

Source

pub const fn position(&self) -> usize

Current position, in bits from the start of the buffer.

Source

pub const fn len_bits(&self) -> usize

Total size of the buffer, in bits.

Source

pub const fn remaining(&self) -> usize

Bits between the cursor and the end of the buffer.

Source

pub const fn seek(&mut self, position: usize)

Moves the cursor to an absolute bit position.

Positions past the end are allowed; the failure surfaces on the next read. XTCE LocationInContainerInBits can legitimately seek forward past unparsed padding.

Source

pub fn skip(&mut self, bits: usize) -> Result<(), BitError>

Advances the cursor without reading.

§Errors

Returns BitError::OutOfBounds if the skip would pass the end of the buffer.

Source

pub fn read_uint(&mut self, width: u32) -> Result<u64, BitError>

Reads width bits as an unsigned integer and advances the cursor.

Width 0 yields 0, matching the reference implementation’s treatment of an empty field. Widths above 64 are rejected.

§Errors

BitError::TooWide above 64 bits, BitError::OutOfBounds past the end.

Source

pub fn peek_uint(&self, position: usize, width: u32) -> Result<u64, BitError>

Reads width bits as an unsigned integer at an absolute position, without moving the cursor.

§Errors

BitError::TooWide above 64 bits, BitError::OutOfBounds past the end.

Source

pub fn peek_next(&self, width: u32) -> Result<u64, BitError>

Reads width bits as an unsigned integer without advancing the cursor.

§Errors

As Self::peek_uint.

Source

pub fn read_bytes(&mut self, width: usize) -> Result<Cow<'a, [u8]>, BitError>

Reads width bits as bytes, right-aligned in the result.

The result is ceil(width / 8) bytes holding the field as a big-endian integer, so a 12-bit field becomes two bytes with four leading zero bits. This is the layout the reference implementation produces for binary fields, and the layout XTCE implies by treating a binary field as an integer of that width.

Byte-aligned whole-byte reads borrow from the packet; everything else allocates.

§Errors

BitError::OutOfBounds past the end of the buffer.

Source

pub fn read_bytes_left_aligned( &mut self, width: usize, ) -> Result<Cow<'a, [u8]>, BitError>

Reads width bits as bytes, left-aligned in the result.

The result is ceil(width / 8) bytes holding the field starting at bit 0, with any slack at the end. This is what a string buffer needs: characters are read from the front, so padding must not displace them.

§Errors

BitError::OutOfBounds past the end of the buffer.

Trait Implementations§

Source§

impl<'a> Clone for BitCursor<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for BitCursor<'a>

Source§

impl<'a> Debug for BitCursor<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for BitCursor<'a>

§

impl<'a> Freeze for BitCursor<'a>

§

impl<'a> Move for BitCursor<'a>

§

impl<'a> RefUnwindSafe for BitCursor<'a>

§

impl<'a> Send for BitCursor<'a>

§

impl<'a> Sync for BitCursor<'a>

§

impl<'a> Unpin for BitCursor<'a>

§

impl<'a> UnsafeUnpin for BitCursor<'a>

§

impl<'a> UnwindSafe for BitCursor<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.