24
24
mod all;
25
25
mod any;
26
26
mod filter_map;
27
+ mod find_map;
27
28
mod min_by;
28
29
mod next;
29
30
mod nth;
@@ -34,6 +35,7 @@ pub use take::Take;
34
35
use all:: AllFuture ;
35
36
use any:: AnyFuture ;
36
37
use filter_map:: FilterMap ;
38
+ use find_map:: FindMapFuture ;
37
39
use min_by:: MinByFuture ;
38
40
use next:: NextFuture ;
39
41
use nth:: NthFuture ;
@@ -52,13 +54,15 @@ cfg_if! {
52
54
( $a: lifetime, $f: tt, $o: ty) => ( ImplFuture <$a, $o>) ;
53
55
( $a: lifetime, $f: tt, $o: ty, $t1: ty) => ( ImplFuture <$a, $o>) ;
54
56
( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty) => ( ImplFuture <$a, $o>) ;
57
+ ( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty, $t3: ty) => ( ImplFuture <$a, $o>) ;
55
58
56
59
}
57
60
} else {
58
61
macro_rules! ret {
59
62
( $a: lifetime, $f: tt, $o: ty) => ( $f<$a, Self >) ;
60
63
( $a: lifetime, $f: tt, $o: ty, $t1: ty) => ( $f<$a, Self , $t1>) ;
61
64
( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty) => ( $f<$a, Self , $t1, $t2>) ;
65
+ ( $a: lifetime, $f: tt, $o: ty, $t1: ty, $t2: ty, $t3: ty) => ( $f<$a, Self , $t1, $t2, $t3>) ;
62
66
}
63
67
}
64
68
}
@@ -317,6 +321,29 @@ pub trait Stream {
317
321
}
318
322
}
319
323
324
+ /// Applies function to the elements of stream and returns the first non-none result.
325
+ ///
326
+ /// ```
327
+ /// # fn main() { async_std::task::block_on(async {
328
+ /// #
329
+ /// use async_std::prelude::*;
330
+ /// use std::collections::VecDeque;
331
+ ///
332
+ /// let mut s: VecDeque<&str> = vec!["lol", "NaN", "2", "5"].into_iter().collect();
333
+ /// let first_number = s.find_map(|s| s.parse().ok()).await;
334
+ ///
335
+ /// assert_eq!(first_number, Some(2));
336
+ /// #
337
+ /// # }) }
338
+ /// ```
339
+ fn find_map < F , B > ( & mut self , f : F ) -> ret ! ( ' _, FindMapFuture , Option <B >, F , Self :: Item , B )
340
+ where
341
+ Self : Sized ,
342
+ F : FnMut ( Self :: Item ) -> Option < B > ,
343
+ {
344
+ FindMapFuture :: new ( self , f)
345
+ }
346
+
320
347
/// Tests if any element of the stream matches a predicate.
321
348
///
322
349
/// `any()` takes a closure that returns `true` or `false`. It applies
0 commit comments