File tree Expand file tree Collapse file tree 3 files changed +37
-34
lines changed Expand file tree Collapse file tree 3 files changed +37
-34
lines changed Original file line number Diff line number Diff line change @@ -17,24 +17,25 @@ where
1717 let stream = stream. into_stream ( ) ;
1818
1919 Box :: pin ( async move {
20- // Using `scan ` here because it is able to stop the stream early
20+ // Using `take_while ` here because it is able to stop the stream early
2121 // if a failure occurs
22- let mut found_error = false ;
22+ let mut found_none = false ;
2323 let out: V = stream
24- . scan ( ( ) , |_, elem| {
25- match elem {
26- Some ( elem) => Some ( elem) ,
27- None => {
28- found_error = true ;
29- // Stop processing the stream on error
30- None
31- }
24+ . take_while ( |elem| {
25+ elem. is_some ( ) || {
26+ found_none = true ;
27+ false
3228 }
3329 } )
30+ . map ( Option :: unwrap)
3431 . collect ( )
3532 . await ;
3633
37- if found_error { None } else { Some ( out) }
34+ if found_none {
35+ None
36+ } else {
37+ Some ( out)
38+ }
3839 } )
3940 }
4041}
Original file line number Diff line number Diff line change 11use std:: pin:: Pin ;
22
33use crate :: prelude:: * ;
4- use crate :: stream:: { Stream , Product } ;
4+ use crate :: stream:: { Product , Stream } ;
55
66impl < T , U > Product < Option < U > > for Option < T >
77where
@@ -36,23 +36,24 @@ where
3636 ```
3737 "# ]
3838 fn product < ' a , S > ( stream : S ) -> Pin < Box < dyn Future < Output = Option < T > > + ' a > >
39- where S : Stream < Item = Option < U > > + ' a
39+ where
40+ S : Stream < Item = Option < U > > + ' a ,
4041 {
4142 Box :: pin ( async move {
42- // Using `scan ` here because it is able to stop the stream early
43+ // Using `take_while ` here because it is able to stop the stream early
4344 // if a failure occurs
4445 let mut found_none = false ;
45- let out = <T as Product < U > >:: product ( stream
46- . scan ( ( ) , |_, elem| {
47- match elem {
48- Some ( elem) => Some ( elem) ,
49- None => {
46+ let out = <T as Product < U > >:: product (
47+ stream
48+ . take_while ( |elem| {
49+ elem. is_some ( ) || {
5050 found_none = true ;
51- // Stop processing the stream on error
52- None
51+ false
5352 }
54- }
55- } ) ) . await ;
53+ } )
54+ . map ( Option :: unwrap) ,
55+ )
56+ . await ;
5657
5758 if found_none {
5859 None
Original file line number Diff line number Diff line change @@ -31,23 +31,24 @@ where
3131 ```
3232 "# ]
3333 fn sum < ' a , S > ( stream : S ) -> Pin < Box < dyn Future < Output = Option < T > > + ' a > >
34- where S : Stream < Item = Option < U > > + ' a
34+ where
35+ S : Stream < Item = Option < U > > + ' a ,
3536 {
3637 Box :: pin ( async move {
37- // Using `scan ` here because it is able to stop the stream early
38+ // Using `take_while ` here because it is able to stop the stream early
3839 // if a failure occurs
3940 let mut found_none = false ;
40- let out = <T as Sum < U > >:: sum ( stream
41- . scan ( ( ) , |_, elem| {
42- match elem {
43- Some ( elem) => Some ( elem) ,
44- None => {
41+ let out = <T as Sum < U > >:: sum (
42+ stream
43+ . take_while ( |elem| {
44+ elem. is_some ( ) || {
4545 found_none = true ;
46- // Stop processing the stream on error
47- None
46+ false
4847 }
49- }
50- } ) ) . await ;
48+ } )
49+ . map ( Option :: unwrap) ,
50+ )
51+ . await ;
5152
5253 if found_none {
5354 None
You can’t perform that action at this time.
0 commit comments