bits_io/
bit_write.rs

1use crate::prelude::*;
2
3pub trait BitWrite: std::io::Write {
4    /// Write a buffer into this writer, returning how many bytes were written.
5    fn write_bits<O: BitStore>(&mut self, source: &BitSlice<O>) -> std::io::Result<usize>;
6
7    /// Write the entirety buf into self.
8    fn write_all_bits<O: BitStore>(&mut self, mut source: &BitSlice<O>) -> std::io::Result<()> {
9        while !source.is_empty() {
10            let n = self.write_bits(source)?;
11            if n == 0 {
12                return Err(std::io::Error::new(
13                    std::io::ErrorKind::WriteZero,
14                    "failed to write all bits",
15                ));
16            }
17            source = &source[n..];
18        }
19        Ok(())
20    }
21}