bit_cursor/
bit_write.rs

1use nsw_types::u1;
2
3pub trait BitWrite: std::io::Write {
4    /// Write a buffer into this writer, returning how many bytes were written.
5    fn write_bits(&mut self, buf: &[u1]) -> std::io::Result<usize>;
6
7    /// Write the entirety buf into self.
8    fn write_all_bits(&mut self, mut buf: &[u1]) -> std::io::Result<()> {
9        while !buf.is_empty() {
10            match self.write_bits(buf) {
11                Ok(0) => {
12                    return Err(std::io::Error::new(
13                        std::io::ErrorKind::UnexpectedEof,
14                        "failed to write whole buffer",
15                    ))
16                }
17                Ok(n) => buf = &buf[n..],
18                Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
19            }
20        }
21
22        Ok(())
23    }
24}