@@ -5,6 +5,7 @@ use pin_project_lite::pin_project;
5
5
6
6
use crate :: prelude:: * ;
7
7
use crate :: stream:: Fuse ;
8
+ use crate :: utils;
8
9
9
10
pin_project ! {
10
11
/// A stream that merges two other streams into a single stream.
@@ -43,19 +44,27 @@ where
43
44
44
45
fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
45
46
let this = self . project ( ) ;
46
- let ( first , second ) = if ( utils:: random ( 1 ) == 1 ) {
47
- ( this. left , this. right )
47
+ if utils:: random ( 1 ) == 1 {
48
+ poll_next_in_order ( cx , this. left , this. right )
48
49
} else {
49
- ( this. right , this. left )
50
- } ;
51
- match first. poll_next ( cx) {
52
- Poll :: Ready ( Some ( item) ) => Poll :: Ready ( Some ( item) ) ,
53
- Poll :: Ready ( None ) => second. poll_next ( cx) ,
54
- Poll :: Pending => match second. poll_next ( cx) {
55
- Poll :: Ready ( Some ( item) ) => Poll :: Ready ( Some ( item) ) ,
56
- Poll :: Ready ( None ) => Poll :: Pending ,
57
- Poll :: Pending => Poll :: Pending ,
58
- } ,
50
+ poll_next_in_order ( cx, this. right , this. left )
59
51
}
60
52
}
61
53
}
54
+
55
+ /// Pools the next item, trying in order, first the first item, then the second one.
56
+ fn poll_next_in_order < F , S , T > ( cx : & mut Context < ' _ > , first : F , second : S ) -> Poll < Option < T > >
57
+ where
58
+ F : Stream < Item = T > ,
59
+ S : Stream < Item = T > ,
60
+ {
61
+ match first. poll_next ( cx) {
62
+ Poll :: Ready ( Some ( item) ) => Poll :: Ready ( Some ( item) ) ,
63
+ Poll :: Ready ( None ) => second. poll_next ( cx) ,
64
+ Poll :: Pending => match second. poll_next ( cx) {
65
+ Poll :: Ready ( Some ( item) ) => Poll :: Ready ( Some ( item) ) ,
66
+ Poll :: Ready ( None ) => Poll :: Pending ,
67
+ Poll :: Pending => Poll :: Pending ,
68
+ } ,
69
+ }
70
+ }
0 commit comments