Skip to main content

chapter_tgz/
count.rs

1use std::io::{Result, Write};
2
3pub(crate) struct Count<W> {
4    out: W,
5    written: u64,
6}
7
8impl<W> Count<W> {
9    pub(crate) fn new(out: W) -> Self {
10        Count { out, written: 0 }
11    }
12
13    pub(crate) fn position(&self) -> u64 {
14        self.written
15    }
16
17    pub(crate) fn into_inner(self) -> W {
18        self.out
19    }
20}
21
22impl<W: Write> Write for Count<W> {
23    fn write(&mut self, buf: &[u8]) -> Result<usize> {
24        let n = self.out.write(buf)?;
25        self.written = self.written.strict_add(u64::try_from(n).unwrap());
26        Ok(n)
27    }
28
29    fn flush(&mut self) -> Result<()> {
30        self.out.flush()
31    }
32}