@@ -5,6 +5,7 @@ use pin_project_lite::pin_project;
55
66use crate :: prelude:: * ;
77use crate :: stream:: Fuse ;
8+ use crate :: utils;
89
910pin_project ! {
1011 /// A stream that merges two other streams into a single stream.
@@ -27,7 +28,10 @@ pin_project! {
2728
2829impl < L : Stream , R : Stream > Merge < L , R > {
2930 pub ( crate ) fn new ( left : L , right : R ) -> Self {
30- Self { left : left. fuse ( ) , right : right. fuse ( ) }
31+ Self {
32+ left : left. fuse ( ) ,
33+ right : right. fuse ( ) ,
34+ }
3135 }
3236}
3337
@@ -40,14 +44,29 @@ where
4044
4145 fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
4246 let this = self . project ( ) ;
43- match this. left . poll_next ( cx) {
44- Poll :: Ready ( Some ( item) ) => Poll :: Ready ( Some ( item) ) ,
45- Poll :: Ready ( None ) => this. right . poll_next ( cx) ,
46- Poll :: Pending => match this. right . poll_next ( cx) {
47- Poll :: Ready ( Some ( item) ) => Poll :: Ready ( Some ( item) ) ,
48- Poll :: Ready ( None ) => Poll :: Pending ,
49- Poll :: Pending => Poll :: Pending ,
50- }
47+ if utils:: random ( 1 ) == 1 {
48+ poll_next_in_order ( this. left , this. right , cx)
49+ } else {
50+ poll_next_in_order ( this. right , this. left , cx)
5151 }
5252 }
5353}
54+
55+ fn poll_next_in_order < F , S , I > (
56+ first : Pin < & mut F > ,
57+ second : Pin < & mut S > ,
58+ cx : & mut Context < ' _ > ,
59+ ) -> Poll < Option < I > >
60+ where
61+ F : Stream < Item = I > ,
62+ S : Stream < Item = I > ,
63+ {
64+ match first. poll_next ( cx) {
65+ Poll :: Ready ( None ) => second. poll_next ( cx) ,
66+ Poll :: Ready ( item) => Poll :: Ready ( item) ,
67+ Poll :: Pending => match second. poll_next ( cx) {
68+ Poll :: Ready ( None ) | Poll :: Pending => Poll :: Pending ,
69+ Poll :: Ready ( item) => Poll :: Ready ( item) ,
70+ } ,
71+ }
72+ }
0 commit comments