Guest User

Untitled

a guest
Jun 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. pub struct CountingRead<'a, R: 'a + Read> {
  2. r: &'a mut R,
  3. count: usize,
  4. }
  5.  
  6. impl<'a, R: 'a + Read> CountingRead<'a, R> {
  7. pub fn new(r: &'a mut R) -> Self {
  8. CountingRead { r: r, count: 0 }
  9. }
  10.  
  11. pub fn count(&self) -> usize {
  12. self.count
  13. }
  14.  
  15. pub fn reset(&mut self) {
  16. self.count = 0;
  17. }
  18. }
  19.  
  20. impl<'a, R: 'a + Read> Read for CountingRead<'a, R> {
  21. fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
  22. match self.r.read(buf) {
  23. Ok(n) => {
  24. self.count += n;
  25. Ok(n)
  26. }
  27. err => err,
  28. }
  29. }
  30. }
Add Comment
Please, Sign In to add comment