@@ -36,9 +36,12 @@ use next::NextFuture;
3636
3737use std:: cmp:: Ordering ;
3838use std:: marker:: PhantomData ;
39+ use std:: pin:: Pin ;
3940
4041use cfg_if:: cfg_if;
4142
43+ use crate :: task:: { Context , Poll } ;
44+
4245cfg_if ! {
4346 if #[ cfg( feature = "docs" ) ] {
4447 #[ doc( hidden) ]
@@ -73,6 +76,55 @@ pub trait Stream {
7376 /// The type of items yielded by this stream.
7477 type Item ;
7578
79+ /// Attempts to receive the next item from the stream.
80+ ///
81+ /// There are several possible return values:
82+ ///
83+ /// * `Poll::Pending` means this stream's next value is not ready yet.
84+ /// * `Poll::Ready(None)` means this stream has been exhausted.
85+ /// * `Poll::Ready(Some(item))` means `item` was received out of the stream.
86+ ///
87+ /// # Examples
88+ ///
89+ /// ```
90+ /// # fn main() { async_std::task::block_on(async {
91+ /// #
92+ /// use std::pin::Pin;
93+ ///
94+ /// use async_std::prelude::*;
95+ /// use async_std::stream;
96+ /// use async_std::task::{Context, Poll};
97+ ///
98+ /// fn increment(s: impl Stream<Item = i32> + Unpin) -> impl Stream<Item = i32> + Unpin {
99+ /// struct Increment<S>(S);
100+ ///
101+ /// impl<S: Stream<Item = i32> + Unpin> Stream for Increment<S> {
102+ /// type Item = S::Item;
103+ ///
104+ /// fn poll_next(
105+ /// mut self: Pin<&mut Self>,
106+ /// cx: &mut Context<'_>,
107+ /// ) -> Poll<Option<Self::Item>> {
108+ /// match Pin::new(&mut self.0).poll_next(cx) {
109+ /// Poll::Pending => Poll::Pending,
110+ /// Poll::Ready(None) => Poll::Ready(None),
111+ /// Poll::Ready(Some(item)) => Poll::Ready(Some(item + 1)),
112+ /// }
113+ /// }
114+ /// }
115+ ///
116+ /// Increment(s)
117+ /// }
118+ ///
119+ /// let mut s = increment(stream::once(7));
120+ ///
121+ /// assert_eq!(s.next().await, Some(8));
122+ /// assert_eq!(s.next().await, None);
123+ /// #
124+ /// # }) }
125+ /// ```
126+ fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > ;
127+
76128 /// Advances the stream and returns the next value.
77129 ///
78130 /// Returns [`None`] when iteration is finished. Individual stream implementations may
@@ -98,7 +150,10 @@ pub trait Stream {
98150 /// ```
99151 fn next ( & mut self ) -> ret ! ( ' _, NextFuture , Option <Self :: Item >)
100152 where
101- Self : Unpin ;
153+ Self : Unpin ,
154+ {
155+ NextFuture { stream : self }
156+ }
102157
103158 /// Creates a stream that yields its first `n` elements.
104159 ///
@@ -207,13 +262,13 @@ pub trait Stream {
207262 #[ inline]
208263 fn all < F > ( & mut self , f : F ) -> ret ! ( ' _, AllFuture , bool , F , Self :: Item )
209264 where
210- Self : Sized ,
265+ Self : Unpin + Sized ,
211266 F : FnMut ( Self :: Item ) -> bool ,
212267 {
213268 AllFuture {
214269 stream : self ,
215270 result : true , // the default if the empty stream
216- __item : PhantomData ,
271+ _marker : PhantomData ,
217272 f,
218273 }
219274 }
@@ -264,13 +319,13 @@ pub trait Stream {
264319 #[ inline]
265320 fn any < F > ( & mut self , f : F ) -> ret ! ( ' _, AnyFuture , bool , F , Self :: Item )
266321 where
267- Self : Sized ,
322+ Self : Unpin + Sized ,
268323 F : FnMut ( Self :: Item ) -> bool ,
269324 {
270325 AnyFuture {
271326 stream : self ,
272327 result : false , // the default if the empty stream
273- __item : PhantomData ,
328+ _marker : PhantomData ,
274329 f,
275330 }
276331 }
@@ -279,10 +334,7 @@ pub trait Stream {
279334impl < T : futures_core:: stream:: Stream + Unpin + ?Sized > Stream for T {
280335 type Item = <Self as futures_core:: stream:: Stream >:: Item ;
281336
282- fn next ( & mut self ) -> ret ! ( ' _, NextFuture , Option <Self :: Item >)
283- where
284- Self : Unpin ,
285- {
286- NextFuture { stream : self }
337+ fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
338+ futures_core:: stream:: Stream :: poll_next ( self , cx)
287339 }
288340}
0 commit comments