#![forbid(unsafe_code)] #![allow(dead_code)] #[must_use] pub fn err1() -> std::io::Error { std::io::Error::new(std::io::ErrorKind::Other, "err1") } pub struct FakeReadWriter { results: Vec>, byte_iter: Box>, } impl FakeReadWriter { #[must_use] fn make_byte_iter() -> Box> { Box::new(std::iter::repeat(b"abcdefghijklmnopqrstuvwxyz".iter().copied()).flatten()) } #[must_use] pub fn new(results: Vec>) -> Self { Self { results, byte_iter: Self::make_byte_iter(), } } #[must_use] pub fn empty() -> Self { Self { results: Vec::new(), byte_iter: Self::make_byte_iter(), } } #[must_use] pub fn is_empty(&self) -> bool { self.results.is_empty() } } impl std::io::Read for FakeReadWriter { fn read(&mut self, buf: &mut [u8]) -> Result { assert!(!self.results.is_empty(), "unexpected read"); let result = self.results.remove(0); if let Ok(num_read) = result { if num_read > 0 { let src: Vec = std::iter::Iterator::take(&mut self.byte_iter, num_read).collect(); let dest = &mut buf[0..num_read]; dest.copy_from_slice(src.as_slice()); } } result } } impl std::io::Write for FakeReadWriter { fn write(&mut self, _buf: &[u8]) -> Result { assert!(!self.results.is_empty(), "unexpected write"); self.results.remove(0) } fn flush(&mut self) -> Result<(), std::io::Error> { Ok(()) } }