23
23
24
24
mod all;
25
25
mod any;
26
+ mod find_map;
26
27
mod min_by;
27
28
mod next;
28
29
mod take;
@@ -31,6 +32,7 @@ pub use take::Take;
31
32
32
33
use all:: AllFuture ;
33
34
use any:: AnyFuture ;
35
+ use find_map:: FindMapFuture ;
34
36
use min_by:: MinByFuture ;
35
37
use next:: NextFuture ;
36
38
@@ -48,13 +50,15 @@ cfg_if! {
48
50
( $a: lifetime, $f: tt, $o: ty) => ( ImplFuture <$a, $o>) ;
49
51
( $a: lifetime, $f: tt, $o: ty, $t1: ty) => ( ImplFuture <$a, $o>) ;
50
52
( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty) => ( ImplFuture <$a, $o>) ;
53
+ ( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty, $t3: ty) => ( ImplFuture <$a, $o>) ;
51
54
52
55
}
53
56
} else {
54
57
macro_rules! ret {
55
58
( $a: lifetime, $f: tt, $o: ty) => ( $f<$a, Self >) ;
56
59
( $a: lifetime, $f: tt, $o: ty, $t1: ty) => ( $f<$a, Self , $t1>) ;
57
60
( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty) => ( $f<$a, Self , $t1, $t2>) ;
61
+ ( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty, $t3: ty) => ( $f<$a, Self , $t1, $t2, $t3>) ;
58
62
}
59
63
}
60
64
}
@@ -218,6 +222,29 @@ pub trait Stream {
218
222
}
219
223
}
220
224
225
+ /// Applies function to the elements of stream and returns the first non-none result.
226
+ ///
227
+ /// ```
228
+ /// # fn main() { async_std::task::block_on(async {
229
+ /// #
230
+ /// use async_std::prelude::*;
231
+ /// use std::collections::VecDeque;
232
+ ///
233
+ /// let mut s: VecDeque<&str> = vec!["lol", "NaN", "2", "5"].into_iter().collect();
234
+ /// let first_number = s.find_map(|s| s.parse().ok()).await;
235
+ ///
236
+ /// assert_eq!(first_number, Some(2));
237
+ /// #
238
+ /// # }) }
239
+ /// ```
240
+ fn find_map < F , B > ( & mut self , f : F ) -> ret ! ( ' _, FindMapFuture , Option <B >, F , Self :: Item , B )
241
+ where
242
+ Self : Sized ,
243
+ F : FnMut ( Self :: Item ) -> Option < B > ,
244
+ {
245
+ FindMapFuture :: new ( self , f)
246
+ }
247
+
221
248
/// Tests if any element of the stream matches a predicate.
222
249
///
223
250
/// `any()` takes a closure that returns `true` or `false`. It applies
0 commit comments