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
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use {Future, Poll, Async};
use task::{self, Task};
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
let inner = Rc::new(RefCell::new(Inner {
value: None,
tx_task: None,
rx_task: None,
}));
let tx = Sender {
inner: Rc::downgrade(&inner),
};
let rx = Receiver {
state: State::Open(inner),
};
(tx, rx)
}
#[derive(Debug)]
pub struct Sender<T> {
inner: Weak<RefCell<Inner<T>>>,
}
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct Receiver<T> {
state: State<T>,
}
#[derive(Debug)]
enum State<T> {
Open(Rc<RefCell<Inner<T>>>),
Closed(Option<T>),
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Canceled;
#[derive(Debug)]
struct Inner<T> {
value: Option<T>,
tx_task: Option<Task>,
rx_task: Option<Task>,
}
impl<T> Sender<T> {
pub fn send(self, val: T) -> Result<(), T> {
if let Some(inner) = self.inner.upgrade() {
inner.borrow_mut().value = Some(val);
Ok(())
} else {
Err(val)
}
}
pub fn poll_cancel(&mut self) -> Poll<(), ()> {
match self.inner.upgrade() {
Some(inner) => {
inner.borrow_mut().tx_task = Some(task::park());
Ok(Async::NotReady)
}
None => Ok(().into()),
}
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
let inner = match self.inner.upgrade() {
Some(inner) => inner,
None => return,
};
let rx_task = {
let mut borrow = inner.borrow_mut();
borrow.tx_task.take();
borrow.rx_task.take()
};
if let Some(task) = rx_task {
task.unpark();
}
}
}
impl<T> Receiver<T> {
pub fn close(&mut self) {
let (item, task) = match self.state {
State::Open(ref inner) => {
let mut inner = inner.borrow_mut();
drop(inner.rx_task.take());
(inner.value.take(), inner.tx_task.take())
}
State::Closed(_) => return,
};
self.state = State::Closed(item);
if let Some(task) = task {
task.unpark();
}
}
}
impl<T> Future for Receiver<T> {
type Item = T;
type Error = Canceled;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let inner = match self.state {
State::Open(ref mut inner) => inner,
State::Closed(ref mut item) => {
match item.take() {
Some(item) => return Ok(item.into()),
None => return Err(Canceled),
}
}
};
if let Some(val) = inner.borrow_mut().value.take() {
return Ok(Async::Ready(val))
}
if Rc::get_mut(inner).is_some() {
Err(Canceled)
} else {
inner.borrow_mut().rx_task = Some(task::park());
Ok(Async::NotReady)
}
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
self.close();
}
}