pub struct BitWriteStream<'a, E>where
E: Endianness,{ /* private fields */ }Expand description
Stream that provides an a way to write non bit aligned adata
§Examples
use bitbuffer::{BitWriteStream, LittleEndian};
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_bool(false)?;
stream.write_int(123u16, 15)?;Implementations§
Source§impl<'a, E> BitWriteStream<'a, E>where
E: Endianness,
impl<'a, E> BitWriteStream<'a, E>where
E: Endianness,
Sourcepub fn new(data: &'a mut Vec<u8>, endianness: E) -> Self
pub fn new(data: &'a mut Vec<u8>, endianness: E) -> Self
Create a new write stream
§Examples
use bitbuffer::{BitWriteStream, LittleEndian};
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);Sourcepub fn from_slice(data: &'a mut [u8], endianness: E) -> Self
pub fn from_slice(data: &'a mut [u8], endianness: E) -> Self
Create a new write stream
Note that the resulting stream will panic when trying to write more data then fits in the provided slice.
Source§impl<E> BitWriteStream<'_, E>where
E: Endianness,
impl<E> BitWriteStream<'_, E>where
E: Endianness,
Sourcepub fn align(&mut self) -> usize
pub fn align(&mut self) -> usize
Align the stream on the next byte by writing zero bits and returns the amount of bits written
§Examples
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_bool(true)?;
stream.align();
assert_eq!(stream.bit_len(), 8);
assert_eq!(data, [0b0000_0001]);Sourcepub fn write_bool(&mut self, value: bool) -> Result<()>
pub fn write_bool(&mut self, value: bool) -> Result<()>
Write a boolean into the buffer
§Examples
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_bool(true)?;Sourcepub fn write_int<T>(&mut self, value: T, count: usize) -> Result<()>
pub fn write_int<T>(&mut self, value: T, count: usize) -> Result<()>
Write an integer into the buffer
§Examples
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_int(123u16, 15)?;Sourcepub fn write_float<T>(&mut self, value: T) -> Result<()>where
T: Float + UncheckedPrimitiveFloat,
pub fn write_float<T>(&mut self, value: T) -> Result<()>where
T: Float + UncheckedPrimitiveFloat,
Write a float into the buffer
§Examples
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_float(123.15f32)?;Sourcepub fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>
pub fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>
Write a number of bytes into the buffer
§Examples
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_bytes(&[0, 1, 2 ,3])?;Sourcepub fn write_bits(&mut self, bits: &BitReadStream<'_, E>) -> Result<()>
pub fn write_bits(&mut self, bits: &BitReadStream<'_, E>) -> Result<()>
Write bits from a read stream into the buffer
Sourcepub fn write_string(
&mut self,
string: &str,
length: Option<usize>,
) -> Result<()>
pub fn write_string( &mut self, string: &str, length: Option<usize>, ) -> Result<()>
Write a string into the buffer
§Examples
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_string("zero terminated string", None)?;
stream.write_string("fixed size string, zero padded", Some(64))?;Sourcepub fn write_sized<T: BitWriteSized<E>>(
&mut self,
value: &T,
length: usize,
) -> Result<()>
pub fn write_sized<T: BitWriteSized<E>>( &mut self, value: &T, length: usize, ) -> Result<()>
Write the type to stream
Sourcepub fn reserve_length<Err: From<BitError>, F: Fn(&mut BitWriteStream<'_, E>) -> Result<(), Err>>(
&mut self,
length_bit_size: usize,
body_fn: F,
) -> Result<(), Err>
pub fn reserve_length<Err: From<BitError>, F: Fn(&mut BitWriteStream<'_, E>) -> Result<(), Err>>( &mut self, length_bit_size: usize, body_fn: F, ) -> Result<(), Err>
Write the length of a section before the section
Auto Trait Implementations§
impl<'a, E> Freeze for BitWriteStream<'a, E>
impl<'a, E> RefUnwindSafe for BitWriteStream<'a, E>where
E: RefUnwindSafe,
impl<'a, E> Send for BitWriteStream<'a, E>where
E: Send,
impl<'a, E> Sync for BitWriteStream<'a, E>where
E: Sync,
impl<'a, E> Unpin for BitWriteStream<'a, E>where
E: Unpin,
impl<'a, E> !UnwindSafe for BitWriteStream<'a, E>
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