@@ -36,9 +36,12 @@ use next::NextFuture;
36
36
37
37
use std:: cmp:: Ordering ;
38
38
use std:: marker:: PhantomData ;
39
+ use std:: pin:: Pin ;
39
40
40
41
use cfg_if:: cfg_if;
41
42
43
+ use crate :: task:: { Context , Poll } ;
44
+
42
45
cfg_if ! {
43
46
if #[ cfg( feature = "docs" ) ] {
44
47
#[ doc( hidden) ]
@@ -73,6 +76,55 @@ pub trait Stream {
73
76
/// The type of items yielded by this stream.
74
77
type Item ;
75
78
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
+
76
128
/// Advances the stream and returns the next value.
77
129
///
78
130
/// Returns [`None`] when iteration is finished. Individual stream implementations may
@@ -98,7 +150,10 @@ pub trait Stream {
98
150
/// ```
99
151
fn next ( & mut self ) -> ret ! ( ' _, NextFuture , Option <Self :: Item >)
100
152
where
101
- Self : Unpin ;
153
+ Self : Unpin ,
154
+ {
155
+ NextFuture { stream : self }
156
+ }
102
157
103
158
/// Creates a stream that yields its first `n` elements.
104
159
///
@@ -207,13 +262,13 @@ pub trait Stream {
207
262
#[ inline]
208
263
fn all < F > ( & mut self , f : F ) -> ret ! ( ' _, AllFuture , bool , F , Self :: Item )
209
264
where
210
- Self : Sized ,
265
+ Self : Unpin + Sized ,
211
266
F : FnMut ( Self :: Item ) -> bool ,
212
267
{
213
268
AllFuture {
214
269
stream : self ,
215
270
result : true , // the default if the empty stream
216
- __item : PhantomData ,
271
+ _marker : PhantomData ,
217
272
f,
218
273
}
219
274
}
@@ -264,13 +319,13 @@ pub trait Stream {
264
319
#[ inline]
265
320
fn any < F > ( & mut self , f : F ) -> ret ! ( ' _, AnyFuture , bool , F , Self :: Item )
266
321
where
267
- Self : Sized ,
322
+ Self : Unpin + Sized ,
268
323
F : FnMut ( Self :: Item ) -> bool ,
269
324
{
270
325
AnyFuture {
271
326
stream : self ,
272
327
result : false , // the default if the empty stream
273
- __item : PhantomData ,
328
+ _marker : PhantomData ,
274
329
f,
275
330
}
276
331
}
@@ -279,10 +334,7 @@ pub trait Stream {
279
334
impl < T : futures_core:: stream:: Stream + Unpin + ?Sized > Stream for T {
280
335
type Item = <Self as futures_core:: stream:: Stream >:: Item ;
281
336
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)
287
339
}
288
340
}
0 commit comments