[][src]Struct endio_bit::BitWriter

pub struct BitWriter<W: Write> { /* fields omitted */ }

Adds bit-level writing support to something implementing std::io::Write.

This is accomplished through an internal buffer for storing partially written bytes.

When the BitWriter is dropped, the partially written byte will be written out. However, any errors that happen in the process of flushing the buffer when the writer is dropped will be ignored. Code that wishes to handle such errors must manually call flush before the writer is dropped.

Methods

impl<W: Write> BitWriter<W>[src]

Important traits for BitWriter<W>
pub fn new(inner: W) -> BitWriter<W>[src]

Creates a new BitWriter from something implementing Write. This will be used as the underlying object to write to.

Examples

Create a BitWriter writing to bytes in memory:

use endio_bit::BitWriter;

let mut vec = vec![];
let mut writer = BitWriter::new(vec);

pub fn write_bit(&mut self, bit: bool) -> Res<()>[src]

Writes a single bit, writing 1 for true, 0 for false.

pub fn write_bits(&mut self, bits: u8, count: u8) -> Res<()>[src]

Writes 8 bits or less.

The lowest count bits will be used, others will be ignored.

Writing more than 8 bits is intentionally not supported to keep the interface simple and to avoid having to deal with endianness in any way. Writing more can be accomplished by writing bytes and then writing any leftover bits.

Panics

Panics if count > 8.

Examples

use endio_bit::BitWriter;

let mut vec = vec![];
let mut writer = BitWriter::new(vec);
writer.write_bits(31, 5);
let vec = writer.into_inner().unwrap();
//assert_eq!(vec[0], 0xf8);

pub fn is_aligned(&self) -> bool[src]

Returns whether the writer is aligned to the byte boundary.

pub fn align(&mut self) -> Res<()>[src]

Aligns to byte boundary, skipping a partial byte if the BitWriter was not aligned.

pub fn get_ref(&self) -> &W[src]

Gets a reference to the underlying writer.

pub fn get_mut(&mut self) -> &mut W[src]

Gets a mutable reference to the underlying writer.

Mutable operations on the underlying writer will corrupt this BitWriter if it is not aligned, so the reference is only returned if the BitWriter is aligned.

Panics if the BitWriter is not aligned.

pub fn get_mut_unchecked(&mut self) -> &mut W[src]

Gets a mutable reference to the underlying writer.

Use with care: Any writing/seeking/etc operation on the underlying writer will corrupt this BitWriter if it is not aligned.

pub fn into_inner(self) -> Result<W, IntoInnerError<BitWriter<W>>>[src]

Unwraps this BitWriter, returning the underlying writer.

The buffer for partial writes will be flushed before returning the writer. If an error occurs during the flushing it will be returned.

Trait Implementations

impl<W: Write> Drop for BitWriter<W>[src]

Flushes the buffer for unaligned writes before the BitWriter is dropped.

impl<W: Debug + Write> Debug for BitWriter<W>[src]

impl<W: Write> Write for BitWriter<W>[src]

Write bytes to a BitWriter just like to Write, but with bit shifting support for unaligned writes.

Directly maps to Write for aligned writes.

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

Attempts to write an entire buffer into this writer. Read more

fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
[src]

Writes a formatted string into this writer, returning any error encountered. Read more

fn by_ref(&mut self) -> &mut Self
1.0.0
[src]

Creates a "by reference" adaptor for this instance of Write. Read more

Auto Trait Implementations

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

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

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.