lzma_rust/enc/
counting.rs

1use std::{cell::Cell, io::Write, rc::Rc};
2
3pub struct CountingWriter<W> {
4    inner: W,
5    counting: Rc<Cell<usize>>,
6    writed_bytes: usize,
7}
8
9impl<W: Write> CountingWriter<W> {
10    pub fn new(inner: W) -> Self {
11        Self {
12            inner,
13            counting: Rc::new(Cell::new(0)),
14            writed_bytes: 0,
15        }
16    }
17    pub fn writed_bytes(&self) -> usize {
18        self.writed_bytes
19    }
20
21    pub fn counting(&self) -> Rc<Cell<usize>> {
22        Rc::clone(&self.counting)
23    }
24}
25
26impl<W: Write> Write for CountingWriter<W> {
27    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
28        let len = self.inner.write(buf)?;
29        self.writed_bytes += len;
30        self.counting.set(self.writed_bytes);
31        Ok(len)
32    }
33
34    fn flush(&mut self) -> std::io::Result<()> {
35        self.inner.flush()
36    }
37}