use crate::prelude::*;
pub trait BitRead: std::io::Read {
fn read_bits<O: BitStore>(&mut self, dest: &mut BitSlice<O>) -> std::io::Result<usize>;
fn read_bits_exact<O: BitStore>(&mut self, dest: &mut BitSlice<O>) -> std::io::Result<()> {
read_bits_exact_helper(self, dest)
}
}
fn read_bits_exact_helper<R: BitRead + ?Sized, O: BitStore>(
this: &mut R,
mut dest: &mut BitSlice<O>,
) -> std::io::Result<()> {
while !dest.is_empty() {
match this.read_bits(dest) {
Ok(0) => break,
Ok(n) => dest = &mut dest[n..],
Err(e) => return Err(e),
}
}
if !dest.is_empty() {
Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
))
} else {
Ok(())
}
}