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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::{collections::VecDeque, fmt, pin::Pin, task::Context, task::Poll};
use futures_core::Stream;
use futures_sink::Sink;
use super::cell::{Cell, WeakCell};
use crate::{future::poll_fn, task::LocalWaker};
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
let shared = Cell::new(Shared {
has_receiver: true,
buffer: VecDeque::new(),
blocked_recv: LocalWaker::new(),
});
let sender = Sender {
shared: shared.clone(),
};
let receiver = Receiver { shared };
(sender, receiver)
}
#[derive(Debug)]
struct Shared<T> {
buffer: VecDeque<T>,
blocked_recv: LocalWaker,
has_receiver: bool,
}
#[derive(Debug)]
pub struct Sender<T> {
shared: Cell<Shared<T>>,
}
impl<T> Unpin for Sender<T> {}
impl<T> Sender<T> {
pub fn send(&self, item: T) -> Result<(), SendError<T>> {
let shared = self.shared.get_mut();
if !shared.has_receiver {
return Err(SendError(item)); };
shared.buffer.push_back(item);
shared.blocked_recv.wake();
Ok(())
}
pub fn close(&self) {
let shared = self.shared.get_mut();
shared.has_receiver = false;
shared.blocked_recv.wake();
}
pub fn is_closed(&self) -> bool {
self.shared.strong_count() == 1 || !self.shared.get_ref().has_receiver
}
pub fn downgrade(self) -> WeakSender<T> {
WeakSender {
shared: self.shared.downgrade(),
}
}
}
impl<T> Clone for Sender<T> {
fn clone(&self) -> Self {
Sender {
shared: self.shared.clone(),
}
}
}
impl<T> Sink<T> for Sender<T> {
type Error = SendError<T>;
fn poll_ready(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), SendError<T>> {
self.send(item)
}
fn poll_flush(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Result<(), SendError<T>>> {
Poll::Ready(Ok(()))
}
fn poll_close(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
self.close();
Poll::Ready(Ok(()))
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
let count = self.shared.strong_count();
let shared = self.shared.get_mut();
if shared.has_receiver && count == 2 {
shared.blocked_recv.wake();
}
}
}
pub struct WeakSender<T> {
shared: WeakCell<Shared<T>>,
}
impl<T> WeakSender<T> {
pub fn upgrade(&self) -> Option<Sender<T>> {
self.shared.upgrade().map(|shared| Sender { shared })
}
}
#[derive(Debug)]
pub struct Receiver<T> {
shared: Cell<Shared<T>>,
}
impl<T> Receiver<T> {
pub fn sender(&self) -> Sender<T> {
Sender {
shared: self.shared.clone(),
}
}
pub fn close(&self) {
self.shared.get_mut().has_receiver = false;
}
pub fn is_closed(&self) -> bool {
self.shared.strong_count() == 1 || !self.shared.get_ref().has_receiver
}
pub async fn recv(&self) -> Option<T> {
poll_fn(|cx| self.poll_recv(cx)).await
}
pub fn poll_recv(&self, cx: &mut Context<'_>) -> Poll<Option<T>> {
let shared = self.shared.get_mut();
if let Some(msg) = shared.buffer.pop_front() {
Poll::Ready(Some(msg))
} else if shared.has_receiver {
shared.blocked_recv.register(cx.waker());
if self.shared.strong_count() == 1 {
Poll::Ready(None)
} else {
Poll::Pending
}
} else {
Poll::Ready(None)
}
}
}
impl<T> Unpin for Receiver<T> {}
impl<T> Stream for Receiver<T> {
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.poll_recv(cx)
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
let shared = self.shared.get_mut();
shared.buffer.clear();
shared.has_receiver = false;
}
}
pub struct SendError<T>(T);
impl<T> std::error::Error for SendError<T> {}
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple("SendError").field(&"...").finish()
}
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "send failed because receiver is gone")
}
}
impl<T> SendError<T> {
pub fn into_inner(self) -> T {
self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{future::lazy, future::stream_recv, Stream};
use futures_sink::Sink;
#[ntex_macros::rt_test2]
async fn test_mpsc() {
let (tx, mut rx) = channel();
assert!(format!("{:?}", tx).contains("Sender"));
assert!(format!("{:?}", rx).contains("Receiver"));
tx.send("test").unwrap();
assert_eq!(stream_recv(&mut rx).await.unwrap(), "test");
let tx2 = tx.clone();
tx2.send("test2").unwrap();
assert_eq!(stream_recv(&mut rx).await.unwrap(), "test2");
assert_eq!(
lazy(|cx| Pin::new(&mut rx).poll_next(cx)).await,
Poll::Pending
);
drop(tx2);
assert_eq!(
lazy(|cx| Pin::new(&mut rx).poll_next(cx)).await,
Poll::Pending
);
drop(tx);
let (tx, mut rx) = channel::<String>();
tx.close();
assert_eq!(stream_recv(&mut rx).await, None);
let (tx, _rx) = channel::<String>();
let weak_tx = tx.downgrade();
assert!(weak_tx.upgrade().is_some());
let (tx, rx) = channel();
tx.send("test").unwrap();
drop(rx);
assert!(tx.send("test").is_err());
let (tx, _) = channel();
let tx2 = tx.clone();
tx.close();
assert!(tx.send("test").is_err());
assert!(tx2.send("test").is_err());
let err = SendError("test");
assert!(format!("{:?}", err).contains("SendError"));
assert!(format!("{}", err).contains("send failed because receiver is gone"));
assert_eq!(err.into_inner(), "test");
}
#[ntex_macros::rt_test2]
async fn test_sink() {
let (mut tx, mut rx) = channel();
lazy(|cx| {
assert!(Pin::new(&mut tx).poll_ready(cx).is_ready());
assert!(Pin::new(&mut tx).start_send("test").is_ok());
assert!(Pin::new(&mut tx).poll_flush(cx).is_ready());
assert!(Pin::new(&mut tx).poll_close(cx).is_ready());
})
.await;
assert_eq!(stream_recv(&mut rx).await.unwrap(), "test");
assert_eq!(stream_recv(&mut rx).await, None);
}
#[ntex_macros::rt_test2]
async fn test_close() {
let (tx, rx) = channel::<()>();
assert!(!tx.is_closed());
assert!(!rx.is_closed());
tx.close();
assert!(tx.is_closed());
assert!(rx.is_closed());
let (tx, rx) = channel::<()>();
rx.close();
assert!(tx.is_closed());
let (tx, rx) = channel::<()>();
drop(tx);
assert!(rx.is_closed());
let _tx = rx.sender();
assert!(!rx.is_closed());
}
}