use std::io;
use super::{BitWrite, ByteWriter, Endianness, PhantomData};
pub struct BitWriter<W: io::Write, E: Endianness> {
pub(crate) writer: W,
pub(crate) value: u8,
pub(crate) bits: u32,
phantom: PhantomData<E>,
}
impl<W: io::Write, E: Endianness> BitWriter<W, E> {
pub fn new(writer: W) -> BitWriter<W, E> {
BitWriter {
writer,
value: 0,
bits: 0,
phantom: PhantomData,
}
}
pub fn endian(writer: W, _endian: E) -> BitWriter<W, E> {
BitWriter {
writer,
value: 0,
bits: 0,
phantom: PhantomData,
}
}
#[inline]
pub fn into_writer(self) -> W {
self.writer
}
#[inline]
pub fn writer(&mut self) -> Option<&mut W> {
if BitWrite::byte_aligned(self) {
Some(&mut self.writer)
} else {
None
}
}
#[inline]
pub fn aligned_writer(&mut self) -> io::Result<&mut W> {
BitWrite::byte_align(self)?;
Ok(&mut self.writer)
}
#[inline]
pub fn into_bytewriter(self) -> ByteWriter<W, E> {
ByteWriter::new(self.into_writer())
}
#[inline]
pub fn bytewriter(&mut self) -> Option<ByteWriter<&mut W, E>> {
self.writer().map(ByteWriter::new)
}
#[inline(always)]
pub fn flush(&mut self) -> io::Result<()> {
self.writer.flush()
}
}