1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use nsw_types::u1;

pub trait BitWrite: std::io::Write {
    /// Write a buffer into this writer, returning how many bytes were written.
    fn write_bits(&mut self, buf: &[u1]) -> std::io::Result<usize>;

    /// Write the entirety buf into self.
    fn write_all_bits(&mut self, mut buf: &[u1]) -> std::io::Result<()> {
        while !buf.is_empty() {
            match self.write_bits(buf) {
                Ok(0) => {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::UnexpectedEof,
                        "failed to write whole buffer",
                    ))
                }
                Ok(n) => buf = &buf[n..],
                Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
            }
        }

        Ok(())
    }
}