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 stream by_ref #538

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,52 @@ extension_trait! {
}
}

#[doc = r#"
Borrows an stream, rather than consuming it.

This is useful to allow applying stream adaptors while still retaining ownership of the original stream.

# Examples

```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;

let a = vec![1isize, 2, 3];

let stream = stream::from_iter(a);

let sum: isize = stream.take(5).sum().await;

assert_eq!(sum, 6);

// if we try to use stream again, it won't work. The following line
// gives "error: use of moved value: `stream`
// assert_eq!(stream.next(), None);

// let's try that again
let a = vec![1isize, 2, 3];

let mut stream = stream::from_iter(a);

// instead, we add in a .by_ref()
let sum: isize = stream.by_ref().take(2).sum().await;

assert_eq!(sum, 3);

// now this is just fine:
assert_eq!(stream.next().await, Some(3));
assert_eq!(stream.next().await, None);
#
# }) }
```
"#]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe mark this as "unstable"?

I'm quite sure this is fine, but just to be sure all new APIs introduced are marked unstable first, so we can stabilize them later. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! I forget it always...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k-nasa no worries! Thanks for fixing!

fn by_ref(&mut self) -> &mut Self {
self
}

#[doc = r#"
A stream adaptor similar to [`fold`] that holds internal state and produces a new
stream.
Expand Down