use std::io;
#[cfg(feature = "alloc")]
use std::vec::Vec;
use super::{
read_to_vec, BitRead, Endianness, FromBitStream, FromBitStreamWith, Integer, Primitive,
SignedInteger,
};
pub trait BitRead2 {
fn read_bit(&mut self) -> io::Result<bool>;
fn read<I>(&mut self, bits: u32) -> io::Result<I>
where
I: Integer + Sized;
fn read_in<const BITS: u32, I>(&mut self) -> io::Result<I>
where
I: Integer,
{
self.read(BITS)
}
fn read_signed<S>(&mut self, bits: u32) -> io::Result<S>
where
S: SignedInteger;
fn read_signed_in<const BITS: u32, S>(&mut self) -> io::Result<S>
where
S: SignedInteger,
{
self.read_signed(BITS)
}
fn read_to<V>(&mut self) -> io::Result<V>
where
V: Primitive;
fn read_as_to<F, V>(&mut self) -> io::Result<V>
where
F: Endianness,
V: Primitive;
fn skip(&mut self, bits: u32) -> io::Result<()>;
fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> {
for b in buf.iter_mut() {
*b = self.read_in::<8, _>()?;
}
Ok(())
}
#[inline(always)]
#[deprecated(since = "1.8.0", note = "use read_to() method instead")]
fn read_to_bytes<const SIZE: usize>(&mut self) -> io::Result<[u8; SIZE]> {
self.read_to()
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> {
read_to_vec(|buf| self.read_bytes(buf), bytes)
}
fn read_unary0(&mut self) -> io::Result<u32> {
let mut unary = 0;
while self.read_bit()? {
unary += 1;
}
Ok(unary)
}
fn read_unary1(&mut self) -> io::Result<u32> {
let mut unary = 0;
while !(self.read_bit()?) {
unary += 1;
}
Ok(unary)
}
fn parse<F: FromBitStream>(&mut self) -> Result<F, F::Error>
where
Self: BitRead,
{
F::from_reader(self)
}
fn parse_with<'a, F: FromBitStreamWith<'a>>(
&mut self,
context: &F::Context,
) -> Result<F, F::Error>
where
Self: BitRead,
{
F::from_reader(self, context)
}
fn byte_aligned(&self) -> bool;
fn byte_align(&mut self);
#[inline]
fn read_huffman<T>(&mut self) -> io::Result<T::Symbol>
where
T: crate::huffman::FromBits,
{
T::from_bits(|| self.read_bit())
}
}
impl<R: BitRead> BitRead2 for R {
#[inline(always)]
fn read_bit(&mut self) -> io::Result<bool> {
BitRead::read_bit(self)
}
#[inline(always)]
fn read<I>(&mut self, bits: u32) -> io::Result<I>
where
I: Integer + Sized,
{
self.read_var(bits)
}
#[inline(always)]
fn read_in<const BITS: u32, I>(&mut self) -> io::Result<I>
where
I: Integer,
{
BitRead::read::<BITS, I>(self)
}
#[inline(always)]
fn read_signed<S>(&mut self, bits: u32) -> io::Result<S>
where
S: SignedInteger,
{
self.read_signed_var(bits)
}
#[inline(always)]
fn read_signed_in<const BITS: u32, S>(&mut self) -> io::Result<S>
where
S: SignedInteger,
{
BitRead::read_signed::<BITS, S>(self)
}
#[inline(always)]
fn read_to<V>(&mut self) -> io::Result<V>
where
V: Primitive,
{
BitRead::read_to::<V>(self)
}
#[inline(always)]
fn read_as_to<F, V>(&mut self) -> io::Result<V>
where
F: Endianness,
V: Primitive,
{
BitRead::read_as_to::<F, V>(self)
}
#[inline(always)]
fn skip(&mut self, bits: u32) -> io::Result<()> {
BitRead::skip(self, bits)
}
#[inline(always)]
fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> {
BitRead::read_bytes(self, buf)
}
#[inline(always)]
fn read_unary0(&mut self) -> io::Result<u32> {
self.read_unary::<0>()
}
#[inline(always)]
fn read_unary1(&mut self) -> io::Result<u32> {
self.read_unary::<1>()
}
#[inline(always)]
fn byte_aligned(&self) -> bool {
BitRead::byte_aligned(self)
}
#[inline(always)]
fn byte_align(&mut self) {
BitRead::byte_align(self);
}
}