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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::stream::IntoStream;
use crate::utils;
use crate::Merge as MergeTrait;
use futures_core::Stream;
use std::pin::Pin;
use std::task::{Context, Poll};
impl<T, S0, S1> MergeTrait for (S0, S1)
where
S0: IntoStream<Item = T>,
S1: IntoStream<Item = T>,
{
type Item = T;
type Stream = Merge2<T, S0::IntoStream, S1::IntoStream>;
fn merge(self) -> Self::Stream {
Merge2::new((self.0.into_stream(), self.1.into_stream()))
}
}
#[derive(Debug)]
#[pin_project::pin_project]
pub struct Merge2<T, S0, S1>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
{
streams: (S0, S1),
}
impl<T, S0, S1> Merge2<T, S0, S1>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
{
pub(crate) fn new(streams: (S0, S1)) -> Self {
Self { streams }
}
}
impl<T, S0, S1> Stream for Merge2<T, S0, S1>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
{
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
let s0 = unsafe { Pin::new_unchecked(&mut this.streams.0) };
let s1 = unsafe { Pin::new_unchecked(&mut this.streams.1) };
match utils::random(2) {
0 => poll_next_in_order(s0, s1, cx),
1 => poll_next_in_order(s1, s0, cx),
_ => unreachable!(),
}
}
}
fn poll_next_in_order<S0, S1, T>(
s0: Pin<&mut S0>,
s1: Pin<&mut S1>,
cx: &mut Context<'_>,
) -> Poll<Option<T>>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
{
match s0.poll_next(cx) {
Poll::Ready(None) => s1.poll_next(cx),
Poll::Ready(item) => Poll::Ready(item),
Poll::Pending => match s1.poll_next(cx) {
Poll::Ready(None) | Poll::Pending => Poll::Pending,
Poll::Ready(item) => Poll::Ready(item),
},
}
}
impl<T, S0, S1, S2> MergeTrait for (S0, S1, S2)
where
S0: IntoStream<Item = T>,
S1: IntoStream<Item = T>,
S2: IntoStream<Item = T>,
{
type Item = T;
type Stream = Merge3<T, S0::IntoStream, S1::IntoStream, S2::IntoStream>;
fn merge(self) -> Self::Stream {
Merge3::new((
self.0.into_stream(),
self.1.into_stream(),
self.2.into_stream(),
))
}
}
#[derive(Debug)]
#[pin_project::pin_project]
pub struct Merge3<T, S0, S1, S2>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
{
streams: (S0, S1, S2),
}
impl<T, S0, S1, S2> Merge3<T, S0, S1, S2>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
S2: Stream<Item = T>,
{
pub(crate) fn new(streams: (S0, S1, S2)) -> Self {
Self { streams }
}
}
impl<T, S0, S1, S2> Stream for Merge3<T, S0, S1, S2>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
S2: Stream<Item = T>,
{
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
fn poll_next_in_order<S0, S1, S2, T>(
s0: Pin<&mut S0>,
s1: Pin<&mut S1>,
s2: Pin<&mut S2>,
cx: &mut Context<'_>,
) -> Poll<Option<T>>
where
S0: Stream<Item = T>,
S1: Stream<Item = T>,
S2: Stream<Item = T>,
{
match s0.poll_next(cx) {
Poll::Ready(None) => s1.poll_next(cx),
Poll::Ready(item) => Poll::Ready(item),
Poll::Pending => match s1.poll_next(cx) {
Poll::Ready(None) => s2.poll_next(cx),
Poll::Ready(item) => Poll::Ready(item),
Poll::Pending => match s2.poll_next(cx) {
Poll::Ready(None) | Poll::Pending => Poll::Pending,
Poll::Ready(item) => Poll::Ready(item),
},
},
}
}
let this = self.project();
let s0 = unsafe { Pin::new_unchecked(&mut this.streams.0) };
let s1 = unsafe { Pin::new_unchecked(&mut this.streams.1) };
let s2 = unsafe { Pin::new_unchecked(&mut this.streams.2) };
match utils::random(6) {
0 => poll_next_in_order(s0, s1, s2, cx),
1 => poll_next_in_order(s0, s2, s1, cx),
2 => poll_next_in_order(s1, s0, s2, cx),
3 => poll_next_in_order(s1, s2, s0, cx),
4 => poll_next_in_order(s2, s0, s1, cx),
5 => poll_next_in_order(s2, s1, s0, cx),
_ => unreachable!(),
}
}
}