/
lib.rs
201 lines (176 loc) · 5.8 KB
/
lib.rs
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
use futures::{future, ready, Stream};
use std::sync::{Arc, Weak};
use std::task::{Context, Poll};
use std::{fmt, future::Future, mem, pin::Pin};
use tokio::sync::{mpsc, OwnedSemaphorePermit as Permit, Semaphore};
/// Returns a new pollable, bounded MPSC channel.
///
/// Unlike `tokio::sync`'s `MPSC` channel, this channel exposes a `poll_ready`
/// function, at the cost of an allocation when driving it to readiness.
pub fn channel<T>(buffer: usize) -> (Sender<T>, Receiver<T>) {
assert!(buffer > 0, "mpsc bounded channel requires buffer > 0");
let semaphore = Arc::new(Semaphore::new(buffer));
let (tx, rx) = mpsc::unbounded_channel();
let rx = Receiver {
rx,
semaphore: Arc::downgrade(&semaphore),
buffer,
};
let tx = Sender {
tx,
semaphore,
state: State::Empty,
};
(tx, rx)
}
#[derive(Debug)]
pub struct Sender<T> {
tx: mpsc::UnboundedSender<(T, Permit)>,
semaphore: Arc<Semaphore>,
state: State,
}
#[derive(Debug)]
pub struct Receiver<T> {
rx: mpsc::UnboundedReceiver<(T, Permit)>,
semaphore: Weak<Semaphore>,
buffer: usize,
}
pub enum SendError<T> {
AtCapacity(T),
Closed(T, Closed),
}
enum State {
Waiting(Pin<Box<dyn Future<Output = Permit> + Send + Sync>>),
Acquired(Permit),
Empty,
}
#[derive(Copy, Clone, Debug)]
pub struct Closed(pub(crate) ());
// === impl Sender ===
impl<T> Sender<T> {
pub fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Closed>> {
loop {
self.state = match self.state {
State::Empty => State::Waiting(Box::pin(self.semaphore.clone().acquire_owned())),
State::Waiting(ref mut f) => State::Acquired(ready!(Pin::new(f).poll(cx))),
State::Acquired(_) if self.tx.is_closed() => return Poll::Ready(Err(Closed(()))),
State::Acquired(_) => return Poll::Ready(Ok(())),
}
}
}
pub async fn ready(&mut self) -> Result<(), Closed> {
future::poll_fn(|cx| self.poll_ready(cx)).await
}
pub fn try_send(&mut self, value: T) -> Result<(), SendError<T>> {
if self.tx.is_closed() {
return Err(SendError::Closed(value, Closed(())));
}
self.state = match mem::replace(&mut self.state, State::Empty) {
State::Acquired(_permit) => {
self.tx.send((value, _permit)).ok().expect("was not closed");
return Ok(());
}
state => state,
};
Err(SendError::AtCapacity(value))
}
pub async fn send(&mut self, value: T) -> Result<(), Closed> {
self.ready().await?;
match mem::replace(&mut self.state, State::Empty) {
State::Acquired(_permit) => {
self.tx.send((value, _permit)).ok().expect("was not closed");
Ok(())
}
state => panic!("unexpected state after poll_ready: {:?}", state),
}
}
}
impl<T> Clone for Sender<T> {
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
semaphore: self.semaphore.clone(),
state: State::Empty,
}
}
}
// === impl Receiver ===
impl<T> Receiver<T> {
pub async fn recv(&mut self) -> Option<T> {
self.rx.recv().await.map(|(t, _)| t)
}
pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
let res = ready!(Pin::new(&mut self.rx).poll_next(cx));
Poll::Ready(res.map(|(t, _)| t))
}
}
impl<T> Stream for Receiver<T> {
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let res = ready!(Pin::new(&mut self.as_mut().rx).poll_next(cx));
Poll::Ready(res.map(|(t, _)| t))
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
if let Some(semaphore) = self.semaphore.upgrade() {
// Close the buffer by releasing any senders waiting on channel capacity.
// If more than `usize::MAX >> 3` permits are added to the semaphore, it
// will panic.
const MAX: usize = std::usize::MAX >> 4;
semaphore.add_permits(MAX - self.buffer - semaphore.available_permits());
}
}
}
// === impl State ===
impl fmt::Debug for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(
match self {
State::Acquired(_) => "State::Acquired(..)",
State::Waiting(_) => "State::Waiting(..)",
State::Empty => "State::Empty",
},
f,
)
}
}
// === impl SendError ===
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SendError::AtCapacity(_) => f
.debug_tuple("SendError::AtCapacity")
.field(&format_args!(".."))
.finish(),
SendError::Closed(_, c) => f
.debug_tuple("SendError::Closed")
.field(c)
.field(&format_args!(".."))
.finish(),
}
}
}
impl<T> std::fmt::Display for SendError<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SendError::AtCapacity(_) => fmt::Display::fmt("channel at capacity", f),
SendError::Closed(_, _) => fmt::Display::fmt("channel closed", f),
}
}
}
impl<T> std::error::Error for SendError<T> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
SendError::Closed(_, c) => Some(c),
_ => None,
}
}
}
// === impl Closed ===
impl std::fmt::Display for Closed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "closed")
}
}
impl std::error::Error for Closed {}