From 1b299896e8b540b096d47f0654de855d57c18f2f Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 23 Sep 2025 09:10:29 +0000 Subject: [PATCH] [Sync Iteration] rust/paasio/1 --- solutions/rust/paasio/1/Cargo.toml | 9 ++++ solutions/rust/paasio/1/src/lib.rs | 80 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 solutions/rust/paasio/1/Cargo.toml create mode 100644 solutions/rust/paasio/1/src/lib.rs diff --git a/solutions/rust/paasio/1/Cargo.toml b/solutions/rust/paasio/1/Cargo.toml new file mode 100644 index 0000000..a6924c7 --- /dev/null +++ b/solutions/rust/paasio/1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "paasio" +version = "0.1.0" +edition = "2024" + +# Not all libraries from crates.io are available in Exercism's test runner. +# The full list of available libraries is here: +# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml +[dependencies] diff --git a/solutions/rust/paasio/1/src/lib.rs b/solutions/rust/paasio/1/src/lib.rs new file mode 100644 index 0000000..7a5f165 --- /dev/null +++ b/solutions/rust/paasio/1/src/lib.rs @@ -0,0 +1,80 @@ +use std::io::{Read, Result, Write}; + +pub struct ReadStats { + wrapped:R, + bytes_read:usize, + reads:usize +} + +impl ReadStats { + pub fn new(wrapped: R) -> ReadStats { + Self { + wrapped, + bytes_read:0, + reads:0 + } + } + + pub fn get_ref(&self) -> &R { + &self.wrapped + } + + pub fn bytes_through(&self) -> usize { + self.bytes_read + } + + pub fn reads(&self) -> usize { + self.reads + } +} + +impl Read for ReadStats { + fn read(&mut self, buf: &mut [u8]) -> Result { + let n = self.wrapped.read(buf)?; + self.bytes_read += n; + self.reads += 1; + Ok(n) + } +} + + +pub struct WriteStats { + wrapped:W, + bytes_write:usize, + writes:usize +} + +impl WriteStats { + pub fn new(wrapped: W) -> WriteStats { + Self { + wrapped, + bytes_write:0, + writes:0 + } + } + + pub fn get_ref(&self) -> &W { + &self.wrapped + } + + pub fn bytes_through(&self) -> usize { + self.bytes_write + } + + pub fn writes(&self) -> usize { + self.writes + } +} + +impl Write for WriteStats { + fn write(&mut self, buf: &[u8]) -> Result { + let n = self.wrapped.write(buf)?; + self.bytes_write += n; + self.writes += 1; + Ok(n) + } + + fn flush(&mut self) -> Result<()> { + self.wrapped.flush() + } +} \ No newline at end of file