Skip to main content

BitWriter

Struct BitWriter 

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

A bounded bit-level writer for encoding packed binary data.

The writer never grows the buffer. Writes past the end return BitError::WriteOverflow.

Implementations§

Source§

impl<'a> BitWriter<'a>

Source

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

Creates a new BitWriter over a fixed buffer.

Source

pub const fn bits_written(&self) -> usize

Returns the number of bits written so far.

Source

pub const fn bits_remaining(&self) -> usize

Returns the number of bits remaining in the buffer.

Source

pub fn write_bit(&mut self, value: bool) -> BitResult<()>

Writes a single bit.

Source

pub fn write_bits(&mut self, value: u64, bits: u8) -> BitResult<()>

Writes up to 64 bits from an unsigned integer.

§Errors

Returns BitError::InvalidBitCount if bits > 64. Returns BitError::ValueOutOfRange if value doesn’t fit in bits.

Source

pub fn align_to_byte(&mut self) -> BitResult<()>

Pads with zero bits until the next byte boundary.

Source

pub fn write_u8_aligned(&mut self, value: u8) -> BitResult<()>

Writes a byte-aligned u8.

Source

pub fn write_u16_aligned(&mut self, value: u16) -> BitResult<()>

Writes a byte-aligned u16 (little-endian).

Source

pub fn write_u32_aligned(&mut self, value: u32) -> BitResult<()>

Writes a byte-aligned u32 (little-endian).

Source

pub fn write_u64_aligned(&mut self, value: u64) -> BitResult<()>

Writes a byte-aligned u64 (little-endian).

Source

pub fn write_varu32(&mut self, value: u32) -> BitResult<()>

Writes a byte-aligned varint u32.

Source

pub fn write_vars32(&mut self, value: i32) -> BitResult<()>

Writes a byte-aligned zigzag varint i32.

Source

pub fn finish(self) -> usize

Finishes writing and returns the number of bytes used.

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> 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> !UnwindSafe 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.