Skip to content

Commit 0c37d4a

Browse files
committed
Anonymous function to avoid type issues
1 parent 79bbf49 commit 0c37d4a

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/stream/stream/merge.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use pin_project_lite::pin_project;
55

66
use crate::prelude::*;
77
use crate::stream::Fuse;
8+
use crate::utils;
89

910
pin_project! {
1011
/// A stream that merges two other streams into a single stream.
@@ -43,19 +44,27 @@ where
4344

4445
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
4546
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)
4849
} 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)
5951
}
6052
}
6153
}
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

Comments
 (0)