Merge pull request #538 from k-nasa/stream_by_ref

Add stream by_ref
new-scheduler
Yoshua Wuyts 5 years ago committed by GitHub
commit 693a7257b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1446,6 +1446,54 @@ 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);
#
# }) }
```
"#]
#[cfg(all(feature = "default", feature = "unstable"))]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
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.

Loading…
Cancel
Save