async_unsync/
oneshot.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! An unsync **oneshot** channel implementation.

use core::{
    cell::UnsafeCell,
    fmt,
    future::Future,
    pin::Pin,
    task::{Context, Poll, Waker},
};

#[cfg(feature = "alloc")]
use crate::alloc::rc::Rc;

use crate::error::{SendError, TryRecvError};

/// Creates a new oneshot channel.
pub const fn channel<T>() -> OneshotChannel<T> {
    OneshotChannel(UnsafeCell::new(Slot {
        value: None,
        recv_waker: None,
        close_waker: None,
        closed: false,
    }))
}

/// An error which can occur when receiving on a closed channel.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RecvError;

/// An unsynchronized (`!Sync`), asynchronous oneshot channel.
///
/// This is useful for asynchronously handing a single value from one future to
/// another.
pub struct OneshotChannel<T>(UnsafeCell<Slot<T>>);

impl<T> OneshotChannel<T> {
    /// Splits the channel into borrowing [`SenderRef`] and [`ReceiverRef`]
    /// handles.
    pub fn split(&mut self) -> (SenderRef<'_, T>, ReceiverRef<'_, T>) {
        let slot = &self.0;
        (SenderRef { slot }, ReceiverRef { slot })
    }

    #[cfg(feature = "alloc")]
    /// Splits the channel into owning [`Sender`] and
    /// [`Receiver`] handles.
    ///
    /// This requires one additional allocation over
    /// [`split`](OneshotChannel::split), but avoids potential lifetime
    /// restrictions.
    pub fn into_split(self) -> (Sender<T>, Receiver<T>) {
        let slot = Rc::new(self.0);
        (Sender { slot: Rc::clone(&slot) }, Receiver { slot })
    }
}

#[cfg(feature = "alloc")]
/// An owning handle for sending an element through a split [`OneshotChannel`].
pub struct Sender<T> {
    slot: Rc<UnsafeCell<Slot<T>>>,
}

#[cfg(feature = "alloc")]
impl<T> Sender<T> {
    /// Returns `true` if the channel has been closed.
    pub fn is_closed(&self) -> bool {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).closed }
    }

    /// Polls the channel, resolving if the channel has been closed.
    pub fn poll_closed(&mut self, cx: &mut Context<'_>) -> Poll<()> {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).poll_closed(cx) }
    }

    /// Resolves when the channel is closed.
    pub async fn closed(&mut self) {
        core::future::poll_fn(|cx| self.poll_closed(cx)).await
    }

    /// Sends a value through the channel.
    ///
    /// # Errors
    ///
    /// Fails, if the channel is closed.
    pub fn send(self, value: T) -> Result<(), SendError<T>> {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).send(value) }
    }
}

#[cfg(feature = "alloc")]
impl<T> Drop for Sender<T> {
    fn drop(&mut self) {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).closed = true }
    }
}

#[cfg(feature = "alloc")]
impl<T> fmt::Debug for Sender<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // SAFETY: no mutable or aliased access to slot possible
        let value = unsafe { &(*self.slot.get()).value };
        f.debug_struct("Sender")
            .field("is_closed", &self.is_closed())
            .field("value", value)
            .finish_non_exhaustive()
    }
}

/// A borrowing handle for sending an element through a split
/// [`OneshotChannel`].
pub struct SenderRef<'a, T> {
    slot: &'a UnsafeCell<Slot<T>>,
}

impl<'a, T> SenderRef<'a, T> {
    /// Returns `true` if the channel has been closed.
    pub fn is_closed(&self) -> bool {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).closed }
    }

    /// Polls the channel, resolving if the channel has been closed.
    pub fn poll_closed(&mut self, cx: &mut Context<'_>) -> Poll<()> {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).poll_closed(cx) }
    }

    /// Resolves when the channel is closed.
    pub async fn closed(&mut self) {
        core::future::poll_fn(|cx| self.poll_closed(cx)).await
    }

    /// Sends a value through the channel.
    ///
    /// # Errors
    ///
    /// Fails, if the channel is closed.
    pub fn send(self, value: T) -> Result<(), SendError<T>> {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).send(value) }
    }
}

impl<T> Drop for SenderRef<'_, T> {
    fn drop(&mut self) {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).closed = true }
    }
}

impl<T> fmt::Debug for SenderRef<'_, T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // SAFETY: no mutable or aliased access to slot possible
        let value = unsafe { &(*self.slot.get()).value };
        f.debug_struct("SenderRef")
            .field("is_closed", &self.is_closed())
            .field("value", value)
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "alloc")]
/// An owning handle for receiving elements through a split [`OneshotChannel`].
///
/// This receiver implements [`Future`] and can be awaited directly:
///
/// ```
/// use async_unsync::oneshot;
///
/// # async fn example_receiver() {
/// let (tx, rx) = oneshot::channel().into_split();
/// tx.send(()).unwrap();
/// let _ = rx.await;
/// # }
/// ```
pub struct Receiver<T> {
    slot: Rc<UnsafeCell<Slot<T>>>,
}

#[cfg(feature = "alloc")]
impl<T> Receiver<T> {
    /// Returns `true` if the channel has been closed.
    pub fn is_closed(&self) -> bool {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).closed }
    }

    /// Closes the channel, causing any [`closed`](Sender::closed) or subsequent
    /// [`poll_closed`](Sender::poll_closed) calls to resolve and any subsequent
    /// [`send`s](Sender::send) to fail on the corresponding [`Sender`].
    pub fn close(&mut self) {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).close_and_wake() }
    }

    /// Receives an element through the channel.
    ///
    /// # Errors
    ///
    /// Fails, if the channel is [empty](TryRecvError::Empty) or
    /// [disconnected](TryRecvError::Disconnected).
    pub fn try_recv(&mut self) -> Result<T, TryRecvError> {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).try_recv() }
    }
}

// Receiver implements Future, so it can be awaited directly.
#[cfg(feature = "alloc")]
impl<T> Future for Receiver<T> {
    type Output = Result<T, RecvError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let slot = &self.get_mut().slot;
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*slot.get()).poll_recv(cx) }
    }
}

#[cfg(feature = "alloc")]
impl<T> Drop for Receiver<T> {
    fn drop(&mut self) {
        self.close();
    }
}

#[cfg(feature = "alloc")]
impl<T> fmt::Debug for Receiver<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // SAFETY: no mutable or aliased access to slot possible
        let value = unsafe { &(*self.slot.get()).value };
        f.debug_struct("Receiver")
            .field("is_closed", &self.is_closed())
            .field("value", value)
            .finish_non_exhaustive()
    }
}

/// A borrowing handle for receiving elements through a split
/// [`OneshotChannel`].
///
/// # Note
///
///
///
/// This receiver implements [`Future`] and can be awaited directly:
///
/// ```
/// # async fn example_receiver_ref() {
/// let mut chan = async_unsync::oneshot::channel();
/// let (tx, rx) = chan.split();
/// tx.send(()).unwrap();
/// let _ = rx.await;
/// # }
/// ```
pub struct ReceiverRef<'a, T> {
    slot: &'a UnsafeCell<Slot<T>>,
}

impl<T> ReceiverRef<'_, T> {
    /// Returns `true` if the channel has been closed.
    pub fn is_closed(&self) -> bool {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).closed }
    }

    /// Closes the channel, causing any [`closed`](SenderRef::closed) or
    /// subsequent [`poll_closed`](SenderRef::poll_closed) calls to resolve and
    /// any subsequent [`send`s](SenderRef::send) to fail on the corresponding
    /// [`SenderRef`].
    pub fn close(&mut self) {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).close_and_wake() }
    }

    /// Receives an element through the channel.
    ///
    /// # Errors
    ///
    /// Fails, if the channel is [empty](TryRecvError::Empty) or
    /// [disconnected](TryRecvError::Disconnected).
    pub fn try_recv(&mut self) -> Result<T, TryRecvError> {
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { (*self.slot.get()).try_recv() }
    }
}

impl<T> Future for ReceiverRef<'_, T> {
    type Output = Result<T, RecvError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let slot = self.get_mut().slot;
        // SAFETY: no mutable or aliased access to slot possible
        unsafe { &mut *slot.get() }.poll_recv(cx)
    }
}

impl<T> Drop for ReceiverRef<'_, T> {
    fn drop(&mut self) {
        self.close();
    }
}

impl<T> fmt::Debug for ReceiverRef<'_, T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // SAFETY: no mutable or aliased access to slot possible
        let value = unsafe { &(*self.slot.get()).value };
        f.debug_struct("ReceiverRef")
            .field("is_closed", &self.is_closed())
            .field("value", value)
            .finish_non_exhaustive()
    }
}

/// A shared underlying data structure for the internal state of a
/// [`OneshotChannel`].
struct Slot<T> {
    // HINT: it's not worth squeezing all Option tags into a single byte and
    // using MaybeUninits instead. Source: I tried
    value: Option<T>,
    recv_waker: Option<Waker>,
    close_waker: Option<Waker>,
    closed: bool,
}

impl<T> Slot<T> {
    fn send(&mut self, value: T) -> Result<(), SendError<T>> {
        // check, if channel has been closed
        if self.closed {
            return Err(SendError(value));
        }

        // store sent value & wake a possibly registered receiver
        self.value = Some(value);
        if let Some(waker) = &self.recv_waker {
            waker.wake_by_ref();
        }

        Ok(())
    }

    fn close_and_wake(&mut self) {
        if self.closed {
            return;
        }

        self.closed = true;
        if let Some(waker) = &self.close_waker {
            waker.wake_by_ref();
        }
    }

    fn poll_closed(&mut self, cx: &mut Context<'_>) -> Poll<()> {
        if self.closed {
            Poll::Ready(())
        } else {
            self.close_waker = Some(cx.waker().clone());
            Poll::Pending
        }
    }

    fn try_recv(&mut self) -> Result<T, TryRecvError> {
        match self.value.take() {
            Some(value) => Ok(value),
            None => match self.closed {
                true => Err(TryRecvError::Disconnected),
                false => Err(TryRecvError::Empty),
            },
        }
    }

    fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Result<T, RecvError>> {
        match self.try_recv() {
            Ok(value) => Poll::Ready(Ok(value)),
            Err(TryRecvError::Disconnected) => Poll::Ready(Err(RecvError)),
            Err(TryRecvError::Empty) => {
                self.recv_waker = Some(cx.waker().clone());
                Poll::Pending
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{future::Future as _, task::Poll};

    use futures_lite::future;

    #[test]
    fn recv() {
        future::block_on(async {
            let mut chan = super::channel::<i32>();
            let (tx, rx) = chan.split();

            tx.send(-1).unwrap();
            assert_eq!(rx.await, Ok(-1));
        });
    }

    #[test]
    fn split_twice() {
        future::block_on(async {
            let mut chan = super::channel::<()>();
            let (tx, rx) = chan.split();

            tx.send(()).unwrap();
            assert!(rx.await.is_ok());

            let (tx, rx) = chan.split();
            assert!(tx.send(()).is_err());
            assert!(rx.await.is_err());
        });
    }

    #[test]
    fn wake_on_close() {
        future::block_on(async {
            let mut chan = super::channel::<i32>();
            let (tx, mut rx) = chan.split();
            let mut rx = core::pin::pin!(rx);

            // poll once: pending
            core::future::poll_fn(|cx| {
                assert!(rx.as_mut().poll(cx).is_pending());
                Poll::Ready(())
            })
            .await;

            // drop tx & close channel
            drop(tx);

            // receiver should return ready + error
            core::future::poll_fn(move |cx| {
                assert!(rx.as_mut().poll(cx).is_ready());
                Poll::Ready(())
            })
            .await;
        });
    }
}