File tree 2 files changed +72
-0
lines changed
2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ use crate :: task:: { Context , Poll } ;
2
+ use std:: pin:: Pin ;
3
+
4
+ use crate :: stream:: Stream ;
5
+
6
+ #[ doc( hidden) ]
7
+ #[ allow( missing_debug_implementations) ]
8
+ pub struct Enumerate < S > {
9
+ stream : S ,
10
+ i : usize ,
11
+ }
12
+
13
+ impl < S > Enumerate < S > {
14
+ pin_utils:: unsafe_pinned!( stream: S ) ;
15
+ pin_utils:: unsafe_unpinned!( i: usize ) ;
16
+
17
+ pub ( super ) fn new ( stream : S ) -> Self {
18
+ Enumerate { stream, i : 0 }
19
+ }
20
+ }
21
+
22
+ impl < S > futures_core:: stream:: Stream for Enumerate < S >
23
+ where
24
+ S : Stream ,
25
+ {
26
+ type Item = ( usize , S :: Item ) ;
27
+
28
+ fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
29
+ let next = futures_core:: ready!( self . as_mut( ) . stream( ) . poll_next( cx) ) ;
30
+
31
+ match next {
32
+ Some ( v) => {
33
+ let ret = ( self . i , v) ;
34
+ * self . as_mut ( ) . i ( ) += 1 ;
35
+ Poll :: Ready ( Some ( ret) )
36
+ }
37
+ None => Poll :: Ready ( None ) ,
38
+ }
39
+ }
40
+ }
Original file line number Diff line number Diff line change 23
23
24
24
mod all;
25
25
mod any;
26
+ mod enumerate;
26
27
mod filter_map;
27
28
mod find;
28
29
mod find_map;
@@ -39,6 +40,7 @@ pub use zip::Zip;
39
40
40
41
use all:: AllFuture ;
41
42
use any:: AnyFuture ;
43
+ use enumerate:: Enumerate ;
42
44
use filter_map:: FilterMap ;
43
45
use find:: FindFuture ;
44
46
use find_map:: FindMapFuture ;
@@ -197,6 +199,36 @@ pub trait Stream {
197
199
}
198
200
}
199
201
202
+ /// Creates a stream that gives the current element's count as well as the next value.
203
+ ///
204
+ /// # Overflow behaviour.
205
+ ///
206
+ /// This combinator does no guarding against overflows.
207
+ ///
208
+ /// # Examples
209
+ /// ```
210
+ /// # fn main() { async_std::task::block_on(async {
211
+ /// #
212
+ /// use async_std::prelude::*;
213
+ /// use std::collections::VecDeque;
214
+ ///
215
+ /// let s: VecDeque<_> = vec!['a', 'b', 'c'].into_iter().collect();
216
+ /// let mut s = s.enumerate();
217
+ ///
218
+ /// assert_eq!(s.next().await, Some((0, 'a')));
219
+ /// assert_eq!(s.next().await, Some((1, 'b')));
220
+ /// assert_eq!(s.next().await, Some((2, 'c')));
221
+ /// assert_eq!(s.next().await, None);
222
+ ///
223
+ /// #
224
+ /// # }) }
225
+ fn enumerate ( self ) -> Enumerate < Self >
226
+ where
227
+ Self : Sized ,
228
+ {
229
+ Enumerate :: new ( self )
230
+ }
231
+
200
232
/// Both filters and maps a stream.
201
233
///
202
234
/// # Examples
You can’t perform that action at this time.
0 commit comments