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