Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add io write by_ref #525

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/io/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ 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?;
let mut buffer = Vec::new();
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?;
Expand Down Expand Up @@ -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);
Expand Down
63 changes: 63 additions & 0 deletions src/io/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -322,3 +359,29 @@ 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![]);

{
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

assert_eq!(f.position(), 5);
// original file still usable, read the rest
f.write_all(b" world").await?;
assert_eq!(f.position(), 11);
Ok(())
})
}
}