aboutsummaryrefslogtreecommitdiff
path: root/paasio/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'paasio/src/lib.rs')
-rw-r--r--paasio/src/lib.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/paasio/src/lib.rs b/paasio/src/lib.rs
new file mode 100644
index 0000000..71f4c9d
--- /dev/null
+++ b/paasio/src/lib.rs
@@ -0,0 +1,82 @@
1use std::io::{Read, Result, Write};
2
3pub struct ReadStats<R> {
4 source: R,
5 reads: usize,
6 bytes: usize,
7}
8
9impl<R: Read> ReadStats<R> {
10 pub fn new(wrapped: R) -> ReadStats<R> {
11 ReadStats {
12 source: wrapped,
13 reads: 0,
14 bytes: 0,
15 }
16 }
17
18 pub fn get_ref(&self) -> &R {
19 &self.source
20 }
21
22 pub fn bytes_through(&self) -> usize {
23 self.bytes
24 }
25
26 pub fn reads(&self) -> usize {
27 self.reads
28 }
29}
30
31impl<R: Read> Read for ReadStats<R> {
32 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
33 let bytes = self.source.read(buf)?;
34 self.reads += 1;
35 self.bytes += bytes;
36 Ok(bytes)
37 }
38}
39
40pub struct WriteStats<W> {
41 sink: W,
42 writes: usize,
43 bytes: usize,
44}
45
46impl<W: Write> WriteStats<W> {
47 // _wrapped is ignored because W is not bounded on Debug or Display and therefore
48 // can't be passed through format!(). For actual implementation you will likely
49 // wish to remove the leading underscore so the variable is not ignored.
50 pub fn new(wrapped: W) -> WriteStats<W> {
51 WriteStats {
52 sink: wrapped,
53 writes: 0,
54 bytes: 0,
55 }
56 }
57
58 pub fn get_ref(&self) -> &W {
59 &self.sink
60 }
61
62 pub fn bytes_through(&self) -> usize {
63 self.bytes
64 }
65
66 pub fn writes(&self) -> usize {
67 self.writes
68 }
69}
70
71impl<W: Write> Write for WriteStats<W> {
72 fn write(&mut self, buf: &[u8]) -> Result<usize> {
73 let bytes = self.sink.write(buf)?;
74 self.writes += 1;
75 self.bytes += bytes;
76 Ok(bytes)
77 }
78
79 fn flush(&mut self) -> Result<()> {
80 self.sink.flush()
81 }
82}