1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::Stream;
#[derive(Debug)]
pub struct JoinStream<L, R> {
left: L,
right: R,
}
impl<L, R> Unpin for JoinStream<L, R> {}
impl<L, R> JoinStream<L, R> {
#[doc(hidden)]
pub fn new(left: L, right: R) -> Self {
Self { left, right }
}
}
impl<L, R, T> Stream for JoinStream<L, R>
where
L: Stream<Item = T> + Unpin,
R: Stream<Item = T> + Unpin,
{
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if let Poll::Ready(Some(item)) = Pin::new(&mut self.left).poll_next(cx) {
cx.waker().wake_by_ref();
Poll::Ready(Some(item))
} else {
Pin::new(&mut self.right).poll_next(cx)
}
}
}
#[macro_export]
macro_rules! join_stream {
($stream1:ident, $stream2:ident, $($stream:ident),* $(,)?) => {{
let joined = $crate::JoinStream::new($stream1, $stream2);
$(
let joined = $crate::JoinStream::new(joined, $stream);
)*
joined
}};
}