@@ -30,8 +30,10 @@ mod filter_map;
30
30
mod find;
31
31
mod find_map;
32
32
mod fold;
33
+ mod for_each;
33
34
mod fuse;
34
35
mod inspect;
36
+ mod map;
35
37
mod min_by;
36
38
mod next;
37
39
mod nth;
@@ -41,6 +43,7 @@ mod skip;
41
43
mod skip_while;
42
44
mod step_by;
43
45
mod take;
46
+ mod try_for_each;
44
47
mod zip;
45
48
46
49
use all:: AllFuture ;
@@ -50,15 +53,18 @@ use filter_map::FilterMap;
50
53
use find:: FindFuture ;
51
54
use find_map:: FindMapFuture ;
52
55
use fold:: FoldFuture ;
56
+ use for_each:: ForEachFuture ;
53
57
use min_by:: MinByFuture ;
54
58
use next:: NextFuture ;
55
59
use nth:: NthFuture ;
56
60
use partial_cmp:: PartialCmpFuture ;
61
+ use try_for_each:: TryForEeachFuture ;
57
62
58
63
pub use chain:: Chain ;
59
64
pub use filter:: Filter ;
60
65
pub use fuse:: Fuse ;
61
66
pub use inspect:: Inspect ;
67
+ pub use map:: Map ;
62
68
pub use scan:: Scan ;
63
69
pub use skip:: Skip ;
64
70
pub use skip_while:: SkipWhile ;
@@ -336,6 +342,37 @@ extension_trait! {
336
342
Enumerate :: new( self )
337
343
}
338
344
345
+ #[ doc = r#"
346
+ Takes a closure and creates a stream that calls that closure on every element of this stream.
347
+
348
+ # Examples
349
+
350
+ ```
351
+ # fn main() { async_std::task::block_on(async {
352
+ #
353
+ use async_std::prelude::*;
354
+ use std::collections::VecDeque;
355
+
356
+ let s: VecDeque<_> = vec![1, 2, 3].into_iter().collect();
357
+ let mut s = s.map(|x| 2 * x);
358
+
359
+ assert_eq!(s.next().await, Some(2));
360
+ assert_eq!(s.next().await, Some(4));
361
+ assert_eq!(s.next().await, Some(6));
362
+ assert_eq!(s.next().await, None);
363
+
364
+ #
365
+ # }) }
366
+ ```
367
+ "# ]
368
+ fn map<B , F >( self , f: F ) -> Map <Self , F , Self :: Item , B >
369
+ where
370
+ Self : Sized ,
371
+ F : FnMut ( Self :: Item ) -> B ,
372
+ {
373
+ Map :: new( self , f)
374
+ }
375
+
339
376
#[ doc = r#"
340
377
A combinator that does something with each element in the stream, passing the value
341
378
on.
@@ -752,6 +789,41 @@ extension_trait! {
752
789
FoldFuture :: new( self , init, f)
753
790
}
754
791
792
+ #[ doc = r#"
793
+ Call a closure on each element of the stream.
794
+
795
+ # Examples
796
+
797
+ ```
798
+ # fn main() { async_std::task::block_on(async {
799
+ #
800
+ use async_std::prelude::*;
801
+ use std::collections::VecDeque;
802
+ use std::sync::mpsc::channel;
803
+
804
+ let (tx, rx) = channel();
805
+
806
+ let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
807
+ let sum = s.for_each(move |x| tx.clone().send(x).unwrap()).await;
808
+
809
+ let v: Vec<_> = rx.iter().collect();
810
+
811
+ assert_eq!(v, vec![1, 2, 3]);
812
+ #
813
+ # }) }
814
+ ```
815
+ "# ]
816
+ fn for_each<F >(
817
+ self ,
818
+ f: F ,
819
+ ) -> impl Future <Output = ( ) > [ ForEachFuture <Self , F , Self :: Item >]
820
+ where
821
+ Self : Sized ,
822
+ F : FnMut ( Self :: Item ) ,
823
+ {
824
+ ForEachFuture :: new( self , f)
825
+ }
826
+
755
827
#[ doc = r#"
756
828
Tests if any element of the stream matches a predicate.
757
829
@@ -923,6 +995,51 @@ extension_trait! {
923
995
Skip :: new( self , n)
924
996
}
925
997
998
+ #[ doc = r#"
999
+ Applies a falliable function to each element in a stream, stopping at first error and returning it.
1000
+
1001
+ # Examples
1002
+
1003
+ ```
1004
+ # fn main() { async_std::task::block_on(async {
1005
+ #
1006
+ use std::collections::VecDeque;
1007
+ use std::sync::mpsc::channel;
1008
+ use async_std::prelude::*;
1009
+
1010
+ let (tx, rx) = channel();
1011
+
1012
+ let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
1013
+ let s = s.try_for_each(|v| {
1014
+ if v % 2 == 1 {
1015
+ tx.clone().send(v).unwrap();
1016
+ Ok(())
1017
+ } else {
1018
+ Err("even")
1019
+ }
1020
+ });
1021
+
1022
+ let res = s.await;
1023
+ drop(tx);
1024
+ let values: Vec<_> = rx.iter().collect();
1025
+
1026
+ assert_eq!(values, vec![1]);
1027
+ assert_eq!(res, Err("even"));
1028
+ #
1029
+ # }) }
1030
+ ```
1031
+ "# ]
1032
+ fn try_for_each<F , E >(
1033
+ self ,
1034
+ f: F ,
1035
+ ) -> impl Future <Output = E > [ TryForEeachFuture <Self , F , Self :: Item , E >]
1036
+ where
1037
+ Self : Sized ,
1038
+ F : FnMut ( Self :: Item ) -> Result <( ) , E >,
1039
+ {
1040
+ TryForEeachFuture :: new( self , f)
1041
+ }
1042
+
926
1043
#[ doc = r#"
927
1044
'Zips up' two streams into a single stream of pairs.
928
1045
@@ -1064,15 +1181,15 @@ extension_trait! {
1064
1181
fn partial_cmp<S >(
1065
1182
self ,
1066
1183
other: S
1067
- ) -> impl Future <Output = Option <Ordering >> + ' _ [ PartialCmpFuture <Self , S >]
1184
+ ) -> impl Future <Output = Option <Ordering >> [ PartialCmpFuture <Self , S >]
1068
1185
where
1069
1186
Self : Sized + Stream ,
1070
- S : Stream ,
1071
- Self :: Item : PartialOrd <S :: Item >,
1187
+ S : Stream ,
1188
+ < Self as Stream > :: Item : PartialOrd <S :: Item >,
1072
1189
{
1073
1190
PartialCmpFuture :: new( self , other)
1074
1191
}
1075
-
1192
+
1076
1193
}
1077
1194
1078
1195
impl <S : Stream + Unpin + ?Sized > Stream for Box <S > {
0 commit comments