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