BitWriteStream

Struct BitWriteStream 

Source
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,

Source

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);
Source

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,

Source

pub fn bit_len(&self) -> usize

The number of written bits in the buffer

Source

pub fn byte_len(&self) -> usize

The number of written bytes in the buffer

Source

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]);
Source

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)?;
Source

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)?;
Source

pub fn write_float<T>(&mut self, value: T) -> Result<()>

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)?;
Source

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])?;
Source

pub fn write_bits(&mut self, bits: &BitReadStream<'_, E>) -> Result<()>

Write bits from a read stream into the buffer

Source

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))?;
Source

pub fn write<T: BitWrite<E>>(&mut self, value: &T) -> Result<()>

Write the type to stream

Source

pub fn write_sized<T: BitWriteSized<E>>( &mut self, value: &T, length: usize, ) -> Result<()>

Write the type to stream

Source

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

Source

pub fn reserve_byte_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 in bytes of a section before the section, the section will be 0 padded to an even byte length

Source

pub fn reserve_int<Err: From<BitError>, F: Fn(&mut BitWriteStream<'_, E>) -> Result<u64, Err>>( &mut self, count: usize, body_fn: F, ) -> Result<(), Err>

Reserve the length to write an integer

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