diff options
Diffstat (limited to 'src/common/counting_writer.rs')
-rw-r--r-- | src/common/counting_writer.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/common/counting_writer.rs b/src/common/counting_writer.rs new file mode 100644 index 0000000..db13e36 --- /dev/null +++ b/src/common/counting_writer.rs @@ -0,0 +1,58 @@ +use std::io::Write; +use std::io; + + +pub struct CountingWriter<W: Write> { + underlying: W, + written_bytes: usize, +} + +impl<W: Write> CountingWriter<W> { + pub fn wrap(underlying: W) -> CountingWriter<W> { + CountingWriter { + underlying: underlying, + written_bytes: 0, + } + } + + pub fn written_bytes(&self) -> usize { + self.written_bytes + } + + pub fn finish(mut self) -> io::Result<(W, usize)> { + self.flush()?; + Ok((self.underlying, self.written_bytes)) + } +} + +impl<W: Write> Write for CountingWriter<W> { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let written_size = self.underlying.write(buf)?; + self.written_bytes += written_size; + Ok(written_size) + } + + fn flush(&mut self) -> io::Result<()> { + self.underlying.flush() + } +} + + + +#[cfg(test)] +mod test { + + use super::CountingWriter; + use std::io::Write; + + #[test] + fn test_counting_writer() { + let buffer: Vec<u8> = vec![]; + let mut counting_writer = CountingWriter::wrap(buffer); + let bytes = (0u8..10u8).collect::<Vec<u8>>(); + counting_writer.write_all(&bytes).unwrap(); + let (w, len): (Vec<u8>, usize) = counting_writer.finish().unwrap(); + assert_eq!(len, 10); + assert_eq!(w.len(), 10); + } +} |