@@ -40,6 +40,7 @@ mod skip;
40
40
mod skip_while;
41
41
mod step_by;
42
42
mod take;
43
+ mod try_for_each;
43
44
mod zip;
44
45
45
46
use all:: AllFuture ;
@@ -52,6 +53,7 @@ use fold::FoldFuture;
52
53
use min_by:: MinByFuture ;
53
54
use next:: NextFuture ;
54
55
use nth:: NthFuture ;
56
+ use try_for_each:: TryForEeachFuture ;
55
57
56
58
pub use chain:: Chain ;
57
59
pub use filter:: Filter ;
@@ -66,6 +68,7 @@ pub use zip::Zip;
66
68
67
69
use std:: cmp:: Ordering ;
68
70
use std:: marker:: PhantomData ;
71
+ use std:: ops:: Try ;
69
72
70
73
use cfg_if:: cfg_if;
71
74
@@ -921,6 +924,52 @@ extension_trait! {
921
924
Skip :: new( self , n)
922
925
}
923
926
927
+ #[ doc = r#"
928
+ Applies a falliable function to each element in a stream, stopping at first error and returning it.
929
+
930
+ # Examples
931
+
932
+ ```
933
+ # fn main() { async_std::task::block_on(async {
934
+ #
935
+ use std::collections::VecDeque;
936
+ use std::sync::mpsc::channel;
937
+ use async_std::prelude::*;
938
+
939
+ let (tx, rx) = channel();
940
+
941
+ let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
942
+ let s = s.try_for_each(|v| {
943
+ if v % 2 == 1 {
944
+ tx.clone().send(v).unwrap();
945
+ Ok(())
946
+ } else {
947
+ Err("even")
948
+ }
949
+ });
950
+
951
+ let res = s.await;
952
+ drop(tx);
953
+ let values: Vec<_> = rx.iter().collect();
954
+
955
+ assert_eq!(values, vec![1]);
956
+ assert_eq!(res, Err("even"));
957
+ #
958
+ # }) }
959
+ ```
960
+ "# ]
961
+ fn try_for_each<F , R >(
962
+ self ,
963
+ f: F ,
964
+ ) -> impl Future <Output = R > [ TryForEeachFuture <Self , F , Self :: Item , R >]
965
+ where
966
+ Self : Sized ,
967
+ F : FnMut ( Self :: Item ) -> R ,
968
+ R : Try <Ok = ( ) >,
969
+ {
970
+ TryForEeachFuture :: new( self , f)
971
+ }
972
+
924
973
#[ doc = r#"
925
974
'Zips up' two streams into a single stream of pairs.
926
975
0 commit comments