Skip to content

Add Write::by_ref #314

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/io/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@ 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::prelude::*;
use async_std::fs::File;

let mut buffer = File::create("foo.txt").await?;

let reference = buffer.by_ref();

// we can use reference just like our original buffer
reference.write_all(b"some bytes")?;
#
# Ok(()) }) }
```
"#]
fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
}

impl<T: Write + Unpin + ?Sized> Write for Box<T> {
Expand Down