1use crate::prelude::*;
2
3pub trait BitRead: std::io::Read {
5 fn read_bits<O: BitStore>(&mut self, dest: &mut BitSlice<O>) -> std::io::Result<usize>;
8
9 fn read_bits_exact<O: BitStore>(&mut self, dest: &mut BitSlice<O>) -> std::io::Result<()> {
11 read_bits_exact_helper(self, dest)
13 }
14}
15
16fn read_bits_exact_helper<R: BitRead + ?Sized, O: BitStore>(
18 this: &mut R,
19 mut dest: &mut BitSlice<O>,
20) -> std::io::Result<()> {
21 while !dest.is_empty() {
22 match this.read_bits(dest) {
26 Ok(0) => break,
27 Ok(n) => dest = &mut dest[n..],
28 Err(e) => return Err(e),
29 }
30 }
31 if !dest.is_empty() {
32 Err(std::io::Error::new(
33 std::io::ErrorKind::UnexpectedEof,
34 "failed to fill whole buffer",
35 ))
36 } else {
37 Ok(())
38 }
39}