|
|
|
@ -36,9 +36,12 @@ use next::NextFuture;
|
|
|
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
|
|
|
|
|
use crate::task::{Context, Poll};
|
|
|
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
|
if #[cfg(feature = "docs")] {
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
@ -73,6 +76,55 @@ pub trait Stream {
|
|
|
|
|
/// The type of items yielded by this stream.
|
|
|
|
|
type Item;
|
|
|
|
|
|
|
|
|
|
/// 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>>;
|
|
|
|
|
|
|
|
|
|
/// Advances the stream and returns the next value.
|
|
|
|
|
///
|
|
|
|
|
/// Returns [`None`] when iteration is finished. Individual stream implementations may
|
|
|
|
@ -98,7 +150,10 @@ pub trait Stream {
|
|
|
|
|
/// ```
|
|
|
|
|
fn next(&mut self) -> ret!('_, NextFuture, Option<Self::Item>)
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin;
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
NextFuture { stream: self }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a stream that yields its first `n` elements.
|
|
|
|
|
///
|
|
|
|
@ -207,13 +262,13 @@ pub trait Stream {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn all<F>(&mut self, f: F) -> ret!('_, AllFuture, bool, F, Self::Item)
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
F: FnMut(Self::Item) -> bool,
|
|
|
|
|
{
|
|
|
|
|
AllFuture {
|
|
|
|
|
stream: self,
|
|
|
|
|
result: true, // the default if the empty stream
|
|
|
|
|
__item: PhantomData,
|
|
|
|
|
_marker: PhantomData,
|
|
|
|
|
f,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -264,13 +319,13 @@ pub trait Stream {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn any<F>(&mut self, f: F) -> ret!('_, AnyFuture, bool, F, Self::Item)
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
Self: Unpin + Sized,
|
|
|
|
|
F: FnMut(Self::Item) -> bool,
|
|
|
|
|
{
|
|
|
|
|
AnyFuture {
|
|
|
|
|
stream: self,
|
|
|
|
|
result: false, // the default if the empty stream
|
|
|
|
|
__item: PhantomData,
|
|
|
|
|
_marker: PhantomData,
|
|
|
|
|
f,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -279,10 +334,7 @@ pub trait Stream {
|
|
|
|
|
impl<T: futures_core::stream::Stream + Unpin + ?Sized> Stream for T {
|
|
|
|
|
type Item = <Self as futures_core::stream::Stream>::Item;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> ret!('_, NextFuture, Option<Self::Item>)
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
NextFuture { stream: self }
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
|
futures_core::stream::Stream::poll_next(self, cx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|