bits_io/io/
bit_write.rs

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