@@ -44,9 +44,12 @@ use nth::NthFuture;
44
44
45
45
use std:: cmp:: Ordering ;
46
46
use std:: marker:: PhantomData ;
47
+ use std:: pin:: Pin ;
47
48
48
49
use cfg_if:: cfg_if;
49
50
51
+ use crate :: task:: { Context , Poll } ;
52
+
50
53
cfg_if ! {
51
54
if #[ cfg( feature = "docs" ) ] {
52
55
#[ doc( hidden) ]
@@ -83,6 +86,55 @@ pub trait Stream {
83
86
/// The type of items yielded by this stream.
84
87
type Item ;
85
88
89
+ /// Attempts to receive the next item from the stream.
90
+ ///
91
+ /// There are several possible return values:
92
+ ///
93
+ /// * `Poll::Pending` means this stream's next value is not ready yet.
94
+ /// * `Poll::Ready(None)` means this stream has been exhausted.
95
+ /// * `Poll::Ready(Some(item))` means `item` was received out of the stream.
96
+ ///
97
+ /// # Examples
98
+ ///
99
+ /// ```
100
+ /// # fn main() { async_std::task::block_on(async {
101
+ /// #
102
+ /// use std::pin::Pin;
103
+ ///
104
+ /// use async_std::prelude::*;
105
+ /// use async_std::stream;
106
+ /// use async_std::task::{Context, Poll};
107
+ ///
108
+ /// fn increment(s: impl Stream<Item = i32> + Unpin) -> impl Stream<Item = i32> + Unpin {
109
+ /// struct Increment<S>(S);
110
+ ///
111
+ /// impl<S: Stream<Item = i32> + Unpin> Stream for Increment<S> {
112
+ /// type Item = S::Item;
113
+ ///
114
+ /// fn poll_next(
115
+ /// mut self: Pin<&mut Self>,
116
+ /// cx: &mut Context<'_>,
117
+ /// ) -> Poll<Option<Self::Item>> {
118
+ /// match Pin::new(&mut self.0).poll_next(cx) {
119
+ /// Poll::Pending => Poll::Pending,
120
+ /// Poll::Ready(None) => Poll::Ready(None),
121
+ /// Poll::Ready(Some(item)) => Poll::Ready(Some(item + 1)),
122
+ /// }
123
+ /// }
124
+ /// }
125
+ ///
126
+ /// Increment(s)
127
+ /// }
128
+ ///
129
+ /// let mut s = increment(stream::once(7));
130
+ ///
131
+ /// assert_eq!(s.next().await, Some(8));
132
+ /// assert_eq!(s.next().await, None);
133
+ /// #
134
+ /// # }) }
135
+ /// ```
136
+ fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > ;
137
+
86
138
/// Advances the stream and returns the next value.
87
139
///
88
140
/// Returns [`None`] when iteration is finished. Individual stream implementations may
@@ -108,7 +160,10 @@ pub trait Stream {
108
160
/// ```
109
161
fn next ( & mut self ) -> ret ! ( ' _, NextFuture , Option <Self :: Item >)
110
162
where
111
- Self : Unpin ;
163
+ Self : Unpin ,
164
+ {
165
+ NextFuture { stream : self }
166
+ }
112
167
113
168
/// Creates a stream that yields its first `n` elements.
114
169
///
@@ -312,13 +367,13 @@ pub trait Stream {
312
367
#[ inline]
313
368
fn all < F > ( & mut self , f : F ) -> ret ! ( ' _, AllFuture , bool , F , Self :: Item )
314
369
where
315
- Self : Sized ,
370
+ Self : Unpin + Sized ,
316
371
F : FnMut ( Self :: Item ) -> bool ,
317
372
{
318
373
AllFuture {
319
374
stream : self ,
320
375
result : true , // the default if the empty stream
321
- __item : PhantomData ,
376
+ _marker : PhantomData ,
322
377
f,
323
378
}
324
379
}
@@ -436,13 +491,13 @@ pub trait Stream {
436
491
#[ inline]
437
492
fn any < F > ( & mut self , f : F ) -> ret ! ( ' _, AnyFuture , bool , F , Self :: Item )
438
493
where
439
- Self : Sized ,
494
+ Self : Unpin + Sized ,
440
495
F : FnMut ( Self :: Item ) -> bool ,
441
496
{
442
497
AnyFuture {
443
498
stream : self ,
444
499
result : false , // the default if the empty stream
445
- __item : PhantomData ,
500
+ _marker : PhantomData ,
446
501
f,
447
502
}
448
503
}
@@ -451,10 +506,7 @@ pub trait Stream {
451
506
impl < T : futures_core:: stream:: Stream + Unpin + ?Sized > Stream for T {
452
507
type Item = <Self as futures_core:: stream:: Stream >:: Item ;
453
508
454
- fn next ( & mut self ) -> ret ! ( ' _, NextFuture , Option <Self :: Item >)
455
- where
456
- Self : Unpin ,
457
- {
458
- NextFuture { stream : self }
509
+ fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
510
+ futures_core:: stream:: Stream :: poll_next ( self , cx)
459
511
}
460
512
}
0 commit comments