pub struct BitWriter<'a> { /* private fields */ }Expand description
Writes packed bits to a caller-supplied byte slice, most-significant-bit first.
The cursor zero-fills bytes lazily as it advances, so callers can pass an
uninitialized-looking buffer and read out only the prefix returned by
BitWriter::finish.
§Example
use wire_codec::BitWriter;
let mut storage = [0u8; 1];
let mut w = BitWriter::new(&mut storage);
w.write_bits(0b101, 3).unwrap();
w.write_bits(0b01100, 5).unwrap();
assert_eq!(w.finish(), 1);
assert_eq!(storage[0], 0xAC);Implementations§
Source§impl<'a> BitWriter<'a>
impl<'a> BitWriter<'a>
Sourcepub fn new(bytes: &'a mut [u8]) -> Self
pub fn new(bytes: &'a mut [u8]) -> Self
Wrap bytes in a new bit cursor. Existing contents of bytes are
overwritten as bits are written.
Sourcepub fn bits_written(&self) -> usize
pub fn bits_written(&self) -> usize
Number of bits written so far.
Sourcepub fn align_to_byte(&mut self) -> Result<()>
pub fn align_to_byte(&mut self) -> Result<()>
Pad the cursor with zero bits up to the next byte boundary.
§Errors
Returns Error::BufferFull if rounding up would exceed the backing
slice.
Sourcepub fn write_bits(&mut self, value: u64, n: u32) -> Result<()>
pub fn write_bits(&mut self, value: u64, n: u32) -> Result<()>
Write the low n bits of value into the buffer.
§Errors
Error::BitOverflowifnis0, greater thanMAX_BIT_WIDTH, orvaluehas bits set above positionn.Error::BufferFullif fewer thannbits remain in the buffer.
Trait Implementations§
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> UnsafeUnpin for BitWriter<'a>
impl<'a> !UnwindSafe for BitWriter<'a>
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