|
|
|
@ -143,98 +143,14 @@ cfg_unstable! {
|
|
|
|
|
mod unzip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension_trait! {
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
pub use futures_core::stream::Stream as Stream;
|
|
|
|
|
|
|
|
|
|
use crate::task::{Context, Poll};
|
|
|
|
|
|
|
|
|
|
#[doc = r#"
|
|
|
|
|
An asynchronous stream of values.
|
|
|
|
|
|
|
|
|
|
This trait is a re-export of [`futures::stream::Stream`] and is an async version of
|
|
|
|
|
[`std::iter::Iterator`].
|
|
|
|
|
|
|
|
|
|
The [provided methods] do not really exist in the trait itself, but they become
|
|
|
|
|
available when [`StreamExt`] from the [prelude] is imported:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# #[allow(unused_imports)]
|
|
|
|
|
use async_std::prelude::*;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[`std::iter::Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
|
|
|
|
|
[`futures::stream::Stream`]:
|
|
|
|
|
https://docs.rs/futures/0.3/futures/stream/trait.Stream.html
|
|
|
|
|
[provided methods]: #provided-methods
|
|
|
|
|
[`StreamExt`]: ../prelude/trait.StreamExt.html
|
|
|
|
|
[prelude]: ../prelude/index.html
|
|
|
|
|
"#]
|
|
|
|
|
pub trait Stream {
|
|
|
|
|
#[doc = r#"
|
|
|
|
|
The type of items yielded by this stream.
|
|
|
|
|
"#]
|
|
|
|
|
type Item;
|
|
|
|
|
|
|
|
|
|
#[doc = r#"
|
|
|
|
|
Attempts to receive the next item from the stream.
|
|
|
|
|
|
|
|
|
|
There are several possible return values:
|
|
|
|
|
|
|
|
|
|
* `Poll::Pending` means this stream's next value is not ready yet.
|
|
|
|
|
* `Poll::Ready(None)` means this stream has been exhausted.
|
|
|
|
|
* `Poll::Ready(Some(item))` means `item` was received out of the stream.
|
|
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# fn main() { async_std::task::block_on(async {
|
|
|
|
|
#
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
|
use async_std::prelude::*;
|
|
|
|
|
use async_std::stream;
|
|
|
|
|
use async_std::task::{Context, Poll};
|
|
|
|
|
|
|
|
|
|
fn increment(
|
|
|
|
|
s: impl Stream<Item = i32> + Unpin,
|
|
|
|
|
) -> impl Stream<Item = i32> + Unpin {
|
|
|
|
|
struct Increment<S>(S);
|
|
|
|
|
|
|
|
|
|
impl<S: Stream<Item = i32> + Unpin> Stream for Increment<S> {
|
|
|
|
|
type Item = S::Item;
|
|
|
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
|
) -> Poll<Option<Self::Item>> {
|
|
|
|
|
match Pin::new(&mut self.0).poll_next(cx) {
|
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
|
Poll::Ready(None) => Poll::Ready(None),
|
|
|
|
|
Poll::Ready(Some(item)) => Poll::Ready(Some(item + 1)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Increment(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut s = increment(stream::once(7));
|
|
|
|
|
|
|
|
|
|
assert_eq!(s.next().await, Some(8));
|
|
|
|
|
assert_eq!(s.next().await, None);
|
|
|
|
|
#
|
|
|
|
|
# }) }
|
|
|
|
|
```
|
|
|
|
|
"#]
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[doc = r#"
|
|
|
|
|
#[doc = r#"
|
|
|
|
|
Extension methods for [`Stream`].
|
|
|
|
|
|
|
|
|
|
[`Stream`]: ../stream/trait.Stream.html
|
|
|
|
|
"#]
|
|
|
|
|
pub trait StreamExt: futures_core::stream::Stream {
|
|
|
|
|
"#]
|
|
|
|
|
pub trait StreamExt: Stream {
|
|
|
|
|
#[doc = r#"
|
|
|
|
|
Advances the stream and returns the next value.
|
|
|
|
|
|
|
|
|
@ -260,7 +176,7 @@ extension_trait! {
|
|
|
|
|
# }) }
|
|
|
|
|
```
|
|
|
|
|
"#]
|
|
|
|
|
fn next(&mut self) -> impl Future<Output = Option<Self::Item>> + '_ [NextFuture<'_, Self>]
|
|
|
|
|
fn next(&mut self) -> NextFuture<'_, Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
@ -712,7 +628,7 @@ extension_trait! {
|
|
|
|
|
"#]
|
|
|
|
|
fn last(
|
|
|
|
|
self,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> [LastFuture<Self, Self::Item>]
|
|
|
|
|
) -> LastFuture<Self, Self::Item>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
@ -922,7 +838,7 @@ extension_trait! {
|
|
|
|
|
fn min_by_key<B, F>(
|
|
|
|
|
self,
|
|
|
|
|
key_by: F,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> [MinByKeyFuture<Self, Self::Item, F>]
|
|
|
|
|
) -> MinByKeyFuture<Self, Self::Item, F>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
B: Ord,
|
|
|
|
@ -958,7 +874,7 @@ extension_trait! {
|
|
|
|
|
fn max_by_key<B, F>(
|
|
|
|
|
self,
|
|
|
|
|
key_by: F,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> [MaxByKeyFuture<Self, Self::Item, F>]
|
|
|
|
|
) -> MaxByKeyFuture<Self, Self::Item, F>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
B: Ord,
|
|
|
|
@ -997,7 +913,7 @@ extension_trait! {
|
|
|
|
|
fn min_by<F>(
|
|
|
|
|
self,
|
|
|
|
|
compare: F,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> [MinByFuture<Self, F, Self::Item>]
|
|
|
|
|
) -> MinByFuture<Self, F, Self::Item>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
|
|
|
|
@ -1030,7 +946,7 @@ extension_trait! {
|
|
|
|
|
"#]
|
|
|
|
|
fn max(
|
|
|
|
|
self,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> [MaxFuture<Self, Self::Item>]
|
|
|
|
|
) -> MaxFuture<Self, Self::Item>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
Self::Item: Ord,
|
|
|
|
@ -1063,7 +979,7 @@ extension_trait! {
|
|
|
|
|
"#]
|
|
|
|
|
fn min(
|
|
|
|
|
self,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> [MinFuture<Self, Self::Item>]
|
|
|
|
|
) -> MinFuture<Self, Self::Item>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
Self::Item: Ord,
|
|
|
|
@ -1101,7 +1017,7 @@ extension_trait! {
|
|
|
|
|
fn max_by<F>(
|
|
|
|
|
self,
|
|
|
|
|
compare: F,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> [MaxByFuture<Self, F, Self::Item>]
|
|
|
|
|
) -> MaxByFuture<Self, F, Self::Item>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
|
|
|
|
@ -1165,7 +1081,7 @@ extension_trait! {
|
|
|
|
|
fn nth(
|
|
|
|
|
&mut self,
|
|
|
|
|
n: usize,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> + '_ [NthFuture<'_, Self>]
|
|
|
|
|
) -> NthFuture<'_, Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
{
|
|
|
|
@ -1221,7 +1137,7 @@ extension_trait! {
|
|
|
|
|
fn all<F>(
|
|
|
|
|
&mut self,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = bool> + '_ [AllFuture<'_, Self, F, Self::Item>]
|
|
|
|
|
) -> AllFuture<'_, Self, F, Self::Item>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
F: FnMut(Self::Item) -> bool,
|
|
|
|
@ -1270,7 +1186,7 @@ extension_trait! {
|
|
|
|
|
fn find<P>(
|
|
|
|
|
&mut self,
|
|
|
|
|
p: P,
|
|
|
|
|
) -> impl Future<Output = Option<Self::Item>> + '_ [FindFuture<'_, Self, P>]
|
|
|
|
|
) -> FindFuture<'_, Self, P>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
P: FnMut(&Self::Item) -> bool,
|
|
|
|
@ -1298,7 +1214,7 @@ extension_trait! {
|
|
|
|
|
fn find_map<F, B>(
|
|
|
|
|
&mut self,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = Option<B>> + '_ [FindMapFuture<'_, Self, F>]
|
|
|
|
|
) -> FindMapFuture<'_, Self, F>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
F: FnMut(Self::Item) -> Option<B>,
|
|
|
|
@ -1332,7 +1248,7 @@ extension_trait! {
|
|
|
|
|
self,
|
|
|
|
|
init: B,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = B> [FoldFuture<Self, F, B>]
|
|
|
|
|
) -> FoldFuture<Self, F, B>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
F: FnMut(B, Self::Item) -> B,
|
|
|
|
@ -1369,7 +1285,7 @@ extension_trait! {
|
|
|
|
|
fn partition<B, F>(
|
|
|
|
|
self,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = (B, B)> [PartitionFuture<Self, F, B>]
|
|
|
|
|
) -> PartitionFuture<Self, F, B>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
F: FnMut(&Self::Item) -> bool,
|
|
|
|
@ -1405,7 +1321,7 @@ extension_trait! {
|
|
|
|
|
fn for_each<F>(
|
|
|
|
|
self,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = ()> [ForEachFuture<Self, F>]
|
|
|
|
|
) -> ForEachFuture<Self, F>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
F: FnMut(Self::Item),
|
|
|
|
@ -1461,7 +1377,7 @@ extension_trait! {
|
|
|
|
|
fn any<F>(
|
|
|
|
|
&mut self,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = bool> + '_ [AnyFuture<'_, Self, F, Self::Item>]
|
|
|
|
|
) -> AnyFuture<'_, Self, F, Self::Item>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
F: FnMut(Self::Item) -> bool,
|
|
|
|
@ -1697,7 +1613,7 @@ extension_trait! {
|
|
|
|
|
&mut self,
|
|
|
|
|
init: T,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = Result<T, E>> + '_ [TryFoldFuture<'_, Self, F, T>]
|
|
|
|
|
) -> TryFoldFuture<'_, Self, F, T>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
F: FnMut(B, Self::Item) -> Result<T, E>,
|
|
|
|
@ -1742,7 +1658,7 @@ extension_trait! {
|
|
|
|
|
fn try_for_each<F, E>(
|
|
|
|
|
&mut self,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> impl Future<Output = E> + 'a [TryForEachFuture<'_, Self, F>]
|
|
|
|
|
) -> TryForEachFuture<'_, Self, F>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
F: FnMut(Self::Item) -> Result<(), E>,
|
|
|
|
@ -1824,7 +1740,7 @@ extension_trait! {
|
|
|
|
|
"#]
|
|
|
|
|
#[cfg(feature = "unstable")]
|
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
|
|
|
|
fn unzip<A, B, FromA, FromB>(self) -> impl Future<Output = (FromA, FromB)> [UnzipFuture<Self, FromA, FromB>]
|
|
|
|
|
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
|
|
|
|
|
where
|
|
|
|
|
FromA: Default + Extend<A>,
|
|
|
|
|
FromB: Default + Extend<B>,
|
|
|
|
@ -1888,7 +1804,7 @@ extension_trait! {
|
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
|
|
|
|
fn collect<'a, B>(
|
|
|
|
|
self,
|
|
|
|
|
) -> impl Future<Output = B> + 'a [Pin<Box<dyn Future<Output = B> + 'a + Send>>]
|
|
|
|
|
) -> Pin<Box<dyn Future<Output = B> + 'a + Send>>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + 'a + Send,
|
|
|
|
|
B: FromStream<Self::Item>,
|
|
|
|
@ -1962,7 +1878,7 @@ extension_trait! {
|
|
|
|
|
fn partial_cmp<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = Option<Ordering>> [PartialCmpFuture<Self, S>]
|
|
|
|
|
) -> PartialCmpFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream,
|
|
|
|
|
S: Stream,
|
|
|
|
@ -2002,7 +1918,7 @@ extension_trait! {
|
|
|
|
|
fn position<P>(
|
|
|
|
|
&mut self,
|
|
|
|
|
predicate: P,
|
|
|
|
|
) -> impl Future<Output = Option<usize>> + '_ [PositionFuture<'_, Self, P>]
|
|
|
|
|
) -> PositionFuture<'_, Self, P>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
P: FnMut(Self::Item) -> bool,
|
|
|
|
@ -2040,7 +1956,7 @@ extension_trait! {
|
|
|
|
|
fn cmp<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = Ordering> [CmpFuture<Self, S>]
|
|
|
|
|
) -> CmpFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream,
|
|
|
|
|
S: Stream,
|
|
|
|
@ -2071,7 +1987,7 @@ extension_trait! {
|
|
|
|
|
"#]
|
|
|
|
|
#[cfg(feature = "unstable")]
|
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
|
|
|
|
fn count(self) -> impl Future<Output = usize> [CountFuture<Self>]
|
|
|
|
|
fn count(self) -> CountFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
@ -2106,7 +2022,7 @@ extension_trait! {
|
|
|
|
|
fn ne<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = bool> [NeFuture<Self, S>]
|
|
|
|
|
) -> NeFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
S: Sized + Stream,
|
|
|
|
@ -2143,7 +2059,7 @@ extension_trait! {
|
|
|
|
|
fn ge<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = bool> [GeFuture<Self, S>]
|
|
|
|
|
) -> GeFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream,
|
|
|
|
|
S: Stream,
|
|
|
|
@ -2180,7 +2096,7 @@ extension_trait! {
|
|
|
|
|
fn eq<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = bool> [EqFuture<Self, S>]
|
|
|
|
|
) -> EqFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream,
|
|
|
|
|
S: Sized + Stream,
|
|
|
|
@ -2217,7 +2133,7 @@ extension_trait! {
|
|
|
|
|
fn gt<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = bool> [GtFuture<Self, S>]
|
|
|
|
|
) -> GtFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream,
|
|
|
|
|
S: Stream,
|
|
|
|
@ -2254,7 +2170,7 @@ extension_trait! {
|
|
|
|
|
fn le<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = bool> [LeFuture<Self, S>]
|
|
|
|
|
) -> LeFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream,
|
|
|
|
|
S: Stream,
|
|
|
|
@ -2291,7 +2207,7 @@ extension_trait! {
|
|
|
|
|
fn lt<S>(
|
|
|
|
|
self,
|
|
|
|
|
other: S
|
|
|
|
|
) -> impl Future<Output = bool> [LtFuture<Self, S>]
|
|
|
|
|
) -> LtFuture<Self, S>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream,
|
|
|
|
|
S: Stream,
|
|
|
|
@ -2335,7 +2251,7 @@ extension_trait! {
|
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
|
|
|
|
fn sum<'a, S>(
|
|
|
|
|
self,
|
|
|
|
|
) -> impl Future<Output = S> + 'a [Pin<Box<dyn Future<Output = S> + 'a>>]
|
|
|
|
|
) -> Pin<Box<dyn Future<Output = S> + 'a>>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream<Item = S> + 'a,
|
|
|
|
|
S: Sum<Self::Item>,
|
|
|
|
@ -2381,48 +2297,14 @@ extension_trait! {
|
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
|
|
|
|
fn product<'a, P>(
|
|
|
|
|
self,
|
|
|
|
|
) -> impl Future<Output = P> + 'a [Pin<Box<dyn Future<Output = P> + 'a>>]
|
|
|
|
|
) -> Pin<Box<dyn Future<Output = P> + 'a>>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized + Stream<Item = P> + 'a,
|
|
|
|
|
P: Product,
|
|
|
|
|
{
|
|
|
|
|
Product::product(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: Stream + Unpin + ?Sized> Stream for Box<S> {
|
|
|
|
|
type Item = S::Item;
|
|
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
|
unreachable!("this impl only appears in the rendered docs")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: Stream + Unpin + ?Sized> Stream for &mut S {
|
|
|
|
|
type Item = S::Item;
|
|
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
|
unreachable!("this impl only appears in the rendered docs")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<P> Stream for Pin<P>
|
|
|
|
|
where
|
|
|
|
|
P: DerefMut + Unpin,
|
|
|
|
|
<P as Deref>::Target: Stream,
|
|
|
|
|
{
|
|
|
|
|
type Item = <<P as Deref>::Target as Stream>::Item;
|
|
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
|
unreachable!("this impl only appears in the rendered docs")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: Stream> Stream for std::panic::AssertUnwindSafe<S> {
|
|
|
|
|
type Item = S::Item;
|
|
|
|
|
impl<T: Stream + ?Sized> StreamExt for T {}
|
|
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
|
unreachable!("this impl only appears in the rendered docs")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|