Struct BitWriter

Source
pub struct BitWriter<W: Write, E: Endianness> { /* private fields */ }
Expand description

For writing bit values to an underlying stream in a given endianness.

Because this only writes whole bytes to the underlying stream, it is important that output is byte-aligned before the bitstream writer’s lifetime ends. Partial bytes will be lost if the writer is disposed of before they can be written.

Implementations§

Source§

impl<W: Write, E: Endianness> BitWriter<W, E>

Source

pub fn new(writer: W) -> BitWriter<W, E>

Wraps a BitWriter around something that implements Write

Source

pub fn endian(writer: W, _endian: E) -> BitWriter<W, E>

Wraps a BitWriter around something that implements Write with the given endianness.

Source

pub fn into_writer(self) -> W

Unwraps internal writer and disposes of BitWriter.

§Warning

Any unwritten partial bits are discarded.

Source

pub fn writer(&mut self) -> Option<&mut W>

If stream is byte-aligned, provides mutable reference to internal writer. Otherwise returns None

Source

pub fn aligned_writer(&mut self) -> Result<&mut W>

Returns byte-aligned mutable reference to internal writer.

Bytes aligns stream if it is not already aligned.

§Errors

Passes along any I/O error from the underlying stream.

Source

pub fn into_bytewriter(self) -> ByteWriter<W, E>

Converts BitWriter to ByteWriter in the same endianness.

§Warning

Any written partial bits are discarded.

Source

pub fn bytewriter(&mut self) -> Option<ByteWriter<&mut W, E>>

If stream is byte-aligned, provides temporary ByteWriter in the same endianness. Otherwise returns None

§Warning

Any unwritten bits left over when ByteWriter is dropped are lost.

Source

pub fn flush(&mut self) -> Result<()>

Flushes output stream to disk, if necessary. Any partial bytes are not flushed.

§Errors

Passes along any errors from the underlying stream.

Trait Implementations§

Source§

impl<W: Write, E: Endianness> BitWrite for BitWriter<W, E>

Source§

fn write_bit(&mut self, bit: bool) -> Result<()>

Writes a single bit to the stream. true indicates 1, false indicates 0 Read more
Source§

fn write_unsigned<const BITS: u32, U>(&mut self, value: U) -> Result<()>
where U: UnsignedInteger,

Writes an unsigned value to the stream using the given const number of bits. Read more
Source§

fn write_unsigned_counted<const BITS: u32, U>( &mut self, bits: BitCount<BITS>, value: U, ) -> Result<()>
where U: UnsignedInteger,

Writes a signed value to the stream with the given number of bits. Read more
Source§

fn write_signed_counted<const BITS: u32, S>( &mut self, bits: impl TryInto<SignedBitCount<BITS>>, value: S, ) -> Result<()>
where S: SignedInteger,

Writes an unsigned value to the stream with the given number of bits. Read more
Source§

fn write_signed<const BITS: u32, S>(&mut self, value: S) -> Result<()>
where S: SignedInteger,

Writes a twos-complement signed value to the stream with the given const number of bits. Read more
Source§

fn write_from<V>(&mut self, value: V) -> Result<()>
where V: Primitive,

Writes whole value to the stream whose size in bits is equal to its type’s size. Read more
Source§

fn write_as_from<F, V>(&mut self, value: V) -> Result<()>
where F: Endianness, V: Primitive,

Writes whole value to the stream whose size in bits is equal to its type’s size in an endianness that may be different from the stream’s endianness. Read more
Source§

fn write_unary<const STOP_BIT: u8>(&mut self, value: u32) -> Result<()>

Writes value number of non STOP_BIT bits to the stream and then writes a STOP_BIT. This field is variably-sized. STOP_BIT must be 0 or 1. Read more
Source§

fn write_bytes(&mut self, buf: &[u8]) -> Result<()>

Writes the entirety of a byte buffer to the stream. Read more
Source§

fn byte_aligned(&self) -> bool

Returns true if the stream is aligned at a whole byte. Read more
Source§

fn write<const BITS: u32, I>(&mut self, value: I) -> Result<()>
where I: Integer,

Writes a signed or unsigned value to the stream using the given const number of bits. Read more
Source§

fn write_var<I>(&mut self, bits: u32, value: I) -> Result<()>
where I: Integer,

Writes a signed or unsigned value to the stream using the given number of bits. Read more
Source§

fn write_unsigned_var<U>(&mut self, bits: u32, value: U) -> Result<()>
where U: UnsignedInteger,

Writes an unsigned value to the stream using the given number of bits. Read more
Source§

fn write_signed_var<S>(&mut self, bits: u32, value: S) -> Result<()>
where S: SignedInteger,

Writes a twos-complement signed value to the stream with the given number of bits. Read more
Source§

fn write_count<const MAX: u32>(&mut self, _: BitCount<MAX>) -> Result<()>

Writes the given bit count to the stream with the necessary maximum number of bits. Read more
Source§

fn write_counted<const MAX: u32, I>( &mut self, bits: BitCount<MAX>, value: I, ) -> Result<()>
where I: Integer + Sized,

Writes a signed or unsigned value to the stream with the given number of bits. Read more
Source§

fn write_const<const BITS: u32, const VALUE: u32>(&mut self) -> Result<()>

Writes the given constant value to the stream with the given number of bits. Read more
Source§

fn pad(&mut self, bits: u32) -> Result<()>

Pads the stream by writing 0 over the given number of bits. Read more
Source§

fn build<T: ToBitStream>(&mut self, build: &T) -> Result<(), T::Error>

Builds and writes complex type
Source§

fn build_with<'a, T: ToBitStreamWith<'a>>( &mut self, build: &T, context: &T::Context, ) -> Result<(), T::Error>

Builds and writes complex type with context
Source§

fn byte_align(&mut self) -> Result<()>

Pads the stream with 0 bits until it is aligned at a whole byte. Does nothing if the stream is already aligned. Read more
Source§

fn write_huffman<T>(&mut self, value: T::Symbol) -> Result<()>
where T: ToBits,

Given a symbol, writes its representation to the output stream as bits. Generates no output if the symbol isn’t defined in the Huffman tree. Read more
Source§

fn by_ref(&mut self) -> &mut Self

Creates a “by reference” adaptor for this BitWrite Read more

Auto Trait Implementations§

§

impl<W, E> Freeze for BitWriter<W, E>
where W: Freeze,

§

impl<W, E> RefUnwindSafe for BitWriter<W, E>

§

impl<W, E> Send for BitWriter<W, E>
where W: Send, E: Send,

§

impl<W, E> Sync for BitWriter<W, E>
where W: Sync, E: Sync,

§

impl<W, E> Unpin for BitWriter<W, E>
where W: Unpin, E: Unpin,

§

impl<W, E> UnwindSafe for BitWriter<W, E>
where W: UnwindSafe, E: UnwindSafe,

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<W> BitWrite2 for W
where W: BitWrite,

Source§

fn write_bit(&mut self, bit: bool) -> Result<(), Error>

Writes a single bit to the stream. true indicates 1, false indicates 0 Read more
Source§

fn write<I>(&mut self, bits: u32, value: I) -> Result<(), Error>
where I: Integer,

Writes a signed or unsigned value to the stream using the given number of bits. Read more
Source§

fn write_out<const BITS: u32, I>(&mut self, value: I) -> Result<(), Error>
where I: Integer,

Writes a signed or unsigned value to the stream using the given const number of bits. Read more
Source§

fn write_unsigned<U>(&mut self, bits: u32, value: U) -> Result<(), Error>
where U: UnsignedInteger,

Writes an unsigned value to the stream using the given number of bits. Read more
Source§

fn write_unsigned_out<const BITS: u32, U>( &mut self, value: U, ) -> Result<(), Error>
where U: UnsignedInteger,

Writes an unsigned value to the stream using the given const number of bits. Read more
Source§

fn write_signed<S>(&mut self, bits: u32, value: S) -> Result<(), Error>
where S: SignedInteger,

Writes a twos-complement signed value to the stream with the given number of bits. Read more
Source§

fn write_signed_out<const BITS: u32, S>( &mut self, value: S, ) -> Result<(), Error>
where S: SignedInteger,

Writes a twos-complement signed value to the stream with the given const number of bits. Read more
Source§

fn write_from<V>(&mut self, value: V) -> Result<(), Error>
where V: Primitive,

Writes whole value to the stream whose size in bits is equal to its type’s size. Read more
Source§

fn write_as_from<F, V>(&mut self, value: V) -> Result<(), Error>
where F: Endianness, V: Primitive,

Writes whole value to the stream whose size in bits is equal to its type’s size in an endianness that may be different from the stream’s endianness. Read more
Source§

fn pad(&mut self, bits: u32) -> Result<(), Error>

Pads the stream by writing 0 over the given number of bits. Read more
Source§

fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Error>

Writes the entirety of a byte buffer to the stream. Read more
Source§

fn write_unary0(&mut self, value: u32) -> Result<(), Error>

Writes value number of 1 bits to the stream and then writes a 0 bit. This field is variably-sized. Read more
Source§

fn write_unary1(&mut self, value: u32) -> Result<(), Error>

Writes value number of 0 bits to the stream and then writes a 1 bit. This field is variably-sized. Read more
Source§

fn byte_aligned(&self) -> bool

Returns true if the stream is aligned at a whole byte.
Source§

fn byte_align(&mut self) -> Result<(), Error>

Pads the stream with 0 bits until it is aligned at a whole byte. Does nothing if the stream is already aligned. Read more
Source§

fn build<T: ToBitStream>(&mut self, build: &T) -> Result<(), T::Error>
where Self: BitWrite,

Builds and writes complex type
Source§

fn build_with<'a, T: ToBitStreamWith<'a>>( &mut self, build: &T, context: &T::Context, ) -> Result<(), T::Error>
where Self: BitWrite,

Builds and writes complex type with context
Source§

fn write_huffman<T>(&mut self, value: T::Symbol) -> Result<()>
where T: ToBits,

Given a symbol, writes its representation to the output stream as bits. Generates no output if the symbol isn’t defined in the Huffman tree. 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.