From 185003907860d2d0332aa4b78a18e02f33848aad Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Wed, 13 Nov 2019 12:20:41 +0800 Subject: [PATCH 1/2] Add io write by_ref --- src/io/read/mod.rs | 6 ++--- src/io/write/mod.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/io/read/mod.rs b/src/io/read/mod.rs index 56f632356..f9b4a9304 100644 --- a/src/io/read/mod.rs +++ b/src/io/read/mod.rs @@ -323,7 +323,7 @@ extension_trait! { ```no_run # fn main() -> std::io::Result<()> { async_std::task::block_on(async { # - use async_std::prelude::*; + use async_std::io::prelude::*; use async_std::fs::File; let mut f = File::open("foo.txt").await?; @@ -331,7 +331,7 @@ extension_trait! { let mut other_buffer = Vec::new(); { - let reference = f.by_ref(); + let reference = ReadExt::by_ref(&mut f); // read at most 5 bytes reference.take(5).read_to_end(&mut buffer).await?; @@ -486,7 +486,7 @@ mod tests { let mut other_buffer = Vec::new(); { - let reference = f.by_ref(); + let reference = io::read::ReadExt::by_ref(&mut f); // read at most 5 bytes assert_eq!(reference.take(5).read_to_end(&mut buffer).await?, 5); diff --git a/src/io/write/mod.rs b/src/io/write/mod.rs index eb114344a..6a19a42d5 100644 --- a/src/io/write/mod.rs +++ b/src/io/write/mod.rs @@ -201,6 +201,43 @@ extension_trait! { WriteAllFuture { writer: self, buf } } + + #[doc = r#" + Creates a "by reference" adaptor for this instance of `Write`. + + The returned adaptor also implements `Write` and will simply borrow this + current writer. + + # Examples + + [`File`][file]s implement `Write`: + + [file]: ../fs/struct.File.html + + ```no_run + # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # + use async_std::io::prelude::*; + use async_std::fs::File; + + let mut f = File::open("foo.txt").await?; + + { + let reference = WriteExt::by_ref(&mut f); + + // read at most 5 bytes + reference.write_all(b"hello ").await?; + + } // drop our &mut reference so we can use f again + + // original file still usable, read the rest + f.write_all(b"world").await?; + # + # Ok(()) }) } + ``` + "#] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + #[doc = r#" Writes a formatted string into this writer, returning any error encountered. @@ -322,3 +359,27 @@ extension_trait! { } } } + + +#[cfg(test)] +mod tests { + use crate::io; + use crate::prelude::*; + + #[test] + fn test_write_by_ref() -> io::Result<()> { + crate::task::block_on(async { + let mut f = io::Cursor::new(vec![0u8; 5]); + + { + let reference = io::write::WriteExt::by_ref(&mut f); + + reference.write_all(b"hello").await?; + } // drop our &mut reference so we can use f again + + // original file still usable, read the rest + f.write_all(b" world").await?; + Ok(()) + }) + } +} \ No newline at end of file From 9d41c1fe97ddc8e9e688bfc436fb15ce5eb6cd51 Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Wed, 13 Nov 2019 12:26:50 +0800 Subject: [PATCH 2/2] alter test to a better case --- src/io/write/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/io/write/mod.rs b/src/io/write/mod.rs index 6a19a42d5..e4fe551c3 100644 --- a/src/io/write/mod.rs +++ b/src/io/write/mod.rs @@ -369,7 +369,7 @@ mod tests { #[test] fn test_write_by_ref() -> io::Result<()> { crate::task::block_on(async { - let mut f = io::Cursor::new(vec![0u8; 5]); + let mut f = io::Cursor::new(vec![]); { let reference = io::write::WriteExt::by_ref(&mut f); @@ -377,8 +377,10 @@ mod tests { reference.write_all(b"hello").await?; } // drop our &mut reference so we can use f again + assert_eq!(f.position(), 5); // original file still usable, read the rest f.write_all(b" world").await?; + assert_eq!(f.position(), 11); Ok(()) }) }