lzma_rs/encode/
util.rs

1use std::io;
2
3// A Write computing a digest on the bytes written.
4pub struct CrcDigestWrite<'a, 'b, W, S>
5where
6    W: 'a + io::Write,
7    S: crc::Width,
8{
9    write: &'a mut W,                   // underlying writer
10    digest: &'a mut crc::Digest<'b, S>, // hasher
11}
12
13impl<'a, 'b, W, S> CrcDigestWrite<'a, 'b, W, S>
14where
15    W: io::Write,
16    S: crc::Width,
17{
18    pub fn new(write: &'a mut W, digest: &'a mut crc::Digest<'b, S>) -> Self {
19        Self { write, digest }
20    }
21}
22
23impl<'a, 'b, W> io::Write for CrcDigestWrite<'a, 'b, W, u32>
24where
25    W: io::Write,
26{
27    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
28        let result = self.write.write(buf)?;
29        self.digest.update(&buf[..result]);
30        Ok(result)
31    }
32    fn flush(&mut self) -> io::Result<()> {
33        self.write.flush()
34    }
35}
36
37// A Write counting the bytes written.
38pub struct CountWrite<'a, W>
39where
40    W: 'a + io::Write,
41{
42    write: &'a mut W, // underlying writer
43    count: usize,     // number of bytes written
44}
45
46impl<'a, W> CountWrite<'a, W>
47where
48    W: io::Write,
49{
50    pub fn new(write: &'a mut W) -> Self {
51        Self { write, count: 0 }
52    }
53
54    pub fn count(&self) -> usize {
55        self.count
56    }
57}
58
59impl<'a, W> io::Write for CountWrite<'a, W>
60where
61    W: io::Write,
62{
63    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
64        let result = self.write.write(buf)?;
65        self.count += result;
66        Ok(result)
67    }
68
69    fn flush(&mut self) -> io::Result<()> {
70        self.write.flush()
71    }
72}