From 8a61a513dad5c22bb5596b8918a2d03755d08d1e Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sat, 26 Dec 2020 16:24:44 +0000 Subject: [rust] PaaS I/O --- paasio/.exercism/metadata.json | 1 + paasio/.gitignore | 8 ++ paasio/Cargo.toml | 4 + paasio/README.md | 102 ++++++++++++++++++++++ paasio/src/lib.rs | 82 ++++++++++++++++++ paasio/tests/paasio.rs | 192 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 389 insertions(+) create mode 100644 paasio/.exercism/metadata.json create mode 100644 paasio/.gitignore create mode 100644 paasio/Cargo.toml create mode 100644 paasio/README.md create mode 100644 paasio/src/lib.rs create mode 100644 paasio/tests/paasio.rs diff --git a/paasio/.exercism/metadata.json b/paasio/.exercism/metadata.json new file mode 100644 index 0000000..e6e8012 --- /dev/null +++ b/paasio/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"paasio","id":"6ac29a8082a94a0a91b43185846bcfe7","url":"https://exercism.io/my/solutions/6ac29a8082a94a0a91b43185846bcfe7","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/paasio/.gitignore b/paasio/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/paasio/.gitignore @@ -0,0 +1,8 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +**/*.rs.bk + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +Cargo.lock diff --git a/paasio/Cargo.toml b/paasio/Cargo.toml new file mode 100644 index 0000000..83a8e73 --- /dev/null +++ b/paasio/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2018" +name = "paasio" +version = "0.0.0" diff --git a/paasio/README.md b/paasio/README.md new file mode 100644 index 0000000..03e5a4d --- /dev/null +++ b/paasio/README.md @@ -0,0 +1,102 @@ +# PaaS I/O + +Report network IO statistics. + +You are writing a [PaaS][], and you need a way to bill customers based +on network and filesystem usage. + +Create a wrapper for network connections and files that can report IO +statistics. The wrapper must report: + +- The total number of bytes read/written. +- The total number of read/write operations. + +[PaaS]: http://en.wikipedia.org/wiki/Platform_as_a_service + +## Abstraction over Networks and Files + +Network and file operations are implemented in terms of the [`io::Read`][read] and [`io::Write`][write] traits. It will therefore be necessary to implement those traits for your types. + +[read]: https://doc.rust-lang.org/std/io/trait.Read.html +[write]: https://doc.rust-lang.org/std/io/trait.Write.html + + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +Execute the tests with: + +```bash +$ cargo test +``` + +All but the first test have been ignored. After you get the first test to +pass, open the tests source file which is located in the `tests` directory +and remove the `#[ignore]` flag from the next test and get the tests to pass +again. Each separate test is a function with `#[test]` flag above it. +Continue, until you pass every test. + +If you wish to run all ignored tests without editing the tests source file, use: + +```bash +$ cargo test -- --ignored +``` + +To run a specific test, for example `some_test`, you can use: + +```bash +$ cargo test some_test +``` + +If the specific test is ignored use: + +```bash +$ cargo test some_test -- --ignored +``` + +To learn more about Rust tests refer to the [online test documentation][rust-tests] + +Make sure to read the [Modules][modules] chapter if you +haven't already, it will help you with organizing your files. + +## Further improvements + +After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. + +To format your solution, inside the solution directory use + +```bash +cargo fmt +``` + +To see, if your solution contains some common ineffective use cases, inside the solution directory use + +```bash +cargo clippy --all-targets +``` + +## Submitting the solution + +Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. + +## Feedback, Issues, Pull Requests + +The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! + +If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). + +[help-page]: https://exercism.io/tracks/rust/learning +[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html +[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Source + +Brian Matsuo [https://github.com/bmatsuo](https://github.com/bmatsuo) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. 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 @@ +use std::io::{Read, Result, Write}; + +pub struct ReadStats { + source: R, + reads: usize, + bytes: usize, +} + +impl ReadStats { + pub fn new(wrapped: R) -> ReadStats { + ReadStats { + source: wrapped, + reads: 0, + bytes: 0, + } + } + + pub fn get_ref(&self) -> &R { + &self.source + } + + pub fn bytes_through(&self) -> usize { + self.bytes + } + + pub fn reads(&self) -> usize { + self.reads + } +} + +impl Read for ReadStats { + fn read(&mut self, buf: &mut [u8]) -> Result { + let bytes = self.source.read(buf)?; + self.reads += 1; + self.bytes += bytes; + Ok(bytes) + } +} + +pub struct WriteStats { + sink: W, + writes: usize, + bytes: usize, +} + +impl WriteStats { + // _wrapped is ignored because W is not bounded on Debug or Display and therefore + // can't be passed through format!(). For actual implementation you will likely + // wish to remove the leading underscore so the variable is not ignored. + pub fn new(wrapped: W) -> WriteStats { + WriteStats { + sink: wrapped, + writes: 0, + bytes: 0, + } + } + + pub fn get_ref(&self) -> &W { + &self.sink + } + + pub fn bytes_through(&self) -> usize { + self.bytes + } + + pub fn writes(&self) -> usize { + self.writes + } +} + +impl Write for WriteStats { + fn write(&mut self, buf: &[u8]) -> Result { + let bytes = self.sink.write(buf)?; + self.writes += 1; + self.bytes += bytes; + Ok(bytes) + } + + fn flush(&mut self) -> Result<()> { + self.sink.flush() + } +} diff --git a/paasio/tests/paasio.rs b/paasio/tests/paasio.rs new file mode 100644 index 0000000..6b44199 --- /dev/null +++ b/paasio/tests/paasio.rs @@ -0,0 +1,192 @@ +/// test a few read scenarios +macro_rules! test_read { + ($(#[$attr:meta])* $modname:ident ($input:expr, $len:expr)) => { + mod $modname { + use std::io::{Read, BufReader}; + use paasio::*; + + const CHUNK_SIZE: usize = 2; + + $(#[$attr])* + #[test] + fn test_read_passthrough() { + let data = $input; + let size = $len(&data); + let mut reader = ReadStats::new(data); + + let mut buffer = Vec::with_capacity(size); + let qty_read = reader.read_to_end(&mut buffer); + + assert!(qty_read.is_ok()); + assert_eq!(size, qty_read.unwrap()); + assert_eq!(size, buffer.len()); + // 2: first to read all the data, second to check that + // there wasn't any more pending data which simply didn't + // fit into the existing buffer + assert_eq!(2, reader.reads()); + assert_eq!(size, reader.bytes_through()); + } + + $(#[$attr])* + #[test] + fn test_read_chunks() { + let data = $input; + let size = $len(&data); + let mut reader = ReadStats::new(data); + + let mut buffer = [0_u8; CHUNK_SIZE]; + let mut chunks_read = 0; + while reader.read(&mut buffer[..]).unwrap_or_else(|_| panic!("read failed at chunk {}", chunks_read+1)) > 0 { + chunks_read += 1; + } + + assert_eq!(size / CHUNK_SIZE + std::cmp::min(1, size % CHUNK_SIZE), chunks_read); + // we read once more than the number of chunks, because the final + // read returns 0 new bytes + assert_eq!(1+chunks_read, reader.reads()); + assert_eq!(size, reader.bytes_through()); + } + + $(#[$attr])* + #[test] + fn test_read_buffered_chunks() { + let data = $input; + let size = $len(&data); + let mut reader = BufReader::new(ReadStats::new(data)); + + let mut buffer = [0_u8; CHUNK_SIZE]; + let mut chunks_read = 0; + while reader.read(&mut buffer[..]).unwrap_or_else(|_| panic!("read failed at chunk {}", chunks_read+1)) > 0 { + chunks_read += 1; + } + + assert_eq!(size / CHUNK_SIZE + std::cmp::min(1, size % CHUNK_SIZE), chunks_read); + // the BufReader should smooth out the reads, collecting into + // a buffer and performing only two read operations: + // the first collects everything into the buffer, + // and the second ensures that no data remains + assert_eq!(2, reader.get_ref().reads()); + assert_eq!(size, reader.get_ref().bytes_through()); + } + } + }; +} + +/// test a few write scenarios +macro_rules! test_write { + ($(#[$attr:meta])* $modname:ident ($input:expr, $len:expr)) => { + mod $modname { + use std::io::{self, Write, BufWriter}; + use paasio::*; + + const CHUNK_SIZE: usize = 2; + $(#[$attr])* + #[test] + fn test_write_passthrough() { + let data = $input; + let size = $len(&data); + let mut writer = WriteStats::new(Vec::with_capacity(size)); + let written = writer.write(data); + assert!(written.is_ok()); + assert_eq!(size, written.unwrap()); + assert_eq!(size, writer.bytes_through()); + assert_eq!(1, writer.writes()); + assert_eq!(data, writer.get_ref().as_slice()); + } + + $(#[$attr])* + #[test] + fn test_sink_oneshot() { + let data = $input; + let size = $len(&data); + let mut writer = WriteStats::new(io::sink()); + let written = writer.write(data); + assert!(written.is_ok()); + assert_eq!(size, written.unwrap()); + assert_eq!(size, writer.bytes_through()); + assert_eq!(1, writer.writes()); + } + + $(#[$attr])* + #[test] + fn test_sink_windowed() { + let data = $input; + let size = $len(&data); + let mut writer = WriteStats::new(io::sink()); + + let mut chunk_count = 0; + for chunk in data.chunks(CHUNK_SIZE) { + chunk_count += 1; + let written = writer.write(chunk); + assert!(written.is_ok()); + assert_eq!(CHUNK_SIZE, written.unwrap()); + } + assert_eq!(size, writer.bytes_through()); + assert_eq!(chunk_count, writer.writes()); + } + + $(#[$attr])* + #[test] + fn test_sink_buffered_windowed() { + let data = $input; + let size = $len(&data); + let mut writer = BufWriter::new(WriteStats::new(io::sink())); + + for chunk in data.chunks(CHUNK_SIZE) { + let written = writer.write(chunk); + assert!(written.is_ok()); + assert_eq!(CHUNK_SIZE, written.unwrap()); + } + // at this point, nothing should have yet been passed through to + // our writer + assert_eq!(0, writer.get_ref().bytes_through()); + assert_eq!(0, writer.get_ref().writes()); + + // after flushing, everything should pass through in one go + assert!(writer.flush().is_ok()); + assert_eq!(size, writer.get_ref().bytes_through()); + assert_eq!(1, writer.get_ref().writes()); + } + } + }; +} + +#[test] +fn test_create_stats() { + let mut data: Vec = Vec::new(); + let _ = paasio::ReadStats::new(data.as_slice()); + let _ = paasio::WriteStats::new(data.as_mut_slice()); +} + +test_read!(read_string ( + "Twas brillig, and the slithy toves/Did gyre and gimble in the wabe:/All mimsy were the borogoves,/And the mome raths outgrabe.".as_bytes(), + |d: &[u8]| d.len() +)); +test_write!(write_string ( + "Beware the Jabberwock, my son!/The jaws that bite, the claws that catch!/Beware the Jubjub bird, and shun/The frumious Bandersnatch!".as_bytes(), + |d: &[u8]| d.len() +)); + +test_read!(read_byte_literal( + &[1_u8, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144][..], + |d: &[u8]| d.len() +)); +test_write!(write_byte_literal( + &[2_u8, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,][..], + |d: &[u8]| d.len() +)); + +test_read!(read_file( + ::std::fs::File::open("README.md").expect("readme must be present"), + |f: &::std::fs::File| f.metadata().expect("metadata must be present").len() as usize +)); + +#[test] +fn read_stats_by_ref_returns_wrapped_reader() { + use paasio::ReadStats; + + let input = + "Why, sometimes I've believed as many as six impossible things before breakfast".as_bytes(); + let reader = ReadStats::new(input); + assert_eq!(reader.get_ref(), &input); +} -- cgit v1.2.3