Run `work` on `writer`, but also count the number of bytes written.
(writer: W, work: impl FnOnce(&mut TeeWriter<W, CountWriter>) -> R)
| 331 | impl CountWriter { |
| 332 | /// Run `work` on `writer`, but also count the number of bytes written. |
| 333 | pub fn run<W: BufWriter, R>(writer: W, work: impl FnOnce(&mut TeeWriter<W, CountWriter>) -> R) -> (R, usize) { |
| 334 | let counter = Self::default(); |
| 335 | let mut writer = TeeWriter::new(writer, counter); |
| 336 | let ret = work(&mut writer); |
| 337 | (ret, writer.w2.finish()) |
| 338 | } |
| 339 | |
| 340 | /// Consumes the counter and returns the final count. |
| 341 | pub fn finish(self) -> usize { |