Skip to main content

BitWriter

Struct BitWriter 

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

Writes fields MSB-first into a borrowed mutable byte slice.

Each bit is explicitly set or cleared, so buf need not be zeroed first.

Implementations§

Source§

impl<'a> BitWriter<'a>

Source

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

Create a writer positioned at the first bit of data.

Source

pub fn capacity_bits(&self) -> usize

Total bits the backing buffer can hold.

Source

pub fn bits_written(&self) -> usize

Bits written so far.

Source

pub fn is_byte_aligned(&self) -> bool

true if the cursor sits on a byte boundary.

Source

pub fn write_bits(&mut self, value: u64, n: u32) -> Result<(), BitError>

Write the low n bits (0..=64) of value, MSB-first.

write_bits(_, 0) writes nothing.

§Errors

BitError::TooManyBits if n > 64; BitError::ValueTooWide if value has bits set above bit n-1; BitError::OutOfBounds if the buffer cannot hold n more bits.

Source

pub fn write_bool(&mut self, value: bool) -> Result<(), BitError>

Write a single bit from a bool.

§Errors

BitError::OutOfBounds if the buffer is full.

Source

pub fn align_to_byte(&mut self) -> Result<(), BitError>

Pad with zero bits up to the next byte boundary (no-op if aligned).

§Errors

BitError::OutOfBounds if the buffer cannot hold the padding.

Trait Implementations§

Source§

impl<'a> Debug for BitWriter<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for BitWriter<'a>

§

impl<'a> Freeze for BitWriter<'a>

§

impl<'a> RefUnwindSafe for BitWriter<'a>

§

impl<'a> Send for BitWriter<'a>

§

impl<'a> Sync for BitWriter<'a>

§

impl<'a> Unpin for BitWriter<'a>

§

impl<'a> UnsafeUnpin for BitWriter<'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> 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.