emscripten-futures 0.5.0

A local async executor and awaitable Emscripten operations for Rust WebAssembly applications
Documentation
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
//! Bounded local queue adapted from local-channel 0.1.5's src/mpsc.rs:
//! https://docs.rs/local-channel/0.1.5/src/local_channel/mpsc.rs.html
//!
//! Copyright (c) 2017-NOW Actix Team. Used under the MIT license; see
//! ../../LICENSE-MIT (which includes the upstream copyright notice).
//!
//! Modified to add bounded channels and release state borrows before invoking
//! wakers or dropping queued values.

use core::{
    cell::RefCell,
    fmt,
    future::poll_fn,
    pin::Pin,
    task::{Context, Poll},
};
use std::error::Error;
use std::{collections::VecDeque, rc::Rc};

use futures::{Sink, Stream};
use local_waker::LocalWaker;

/// Creates a unbounded in-memory channel with buffered storage.
///
/// [Sender]s and [Receiver]s are `!Send`.
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
    bounded(usize::MAX)
}

/// Creates a channel with at most `capacity` queued messages across all senders.
/// Sending to a full buffer returns an error, including through `Sink`.
/// Panics if `capacity` is zero.
pub fn bounded<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
    assert!(capacity > 0);
    let shared = Rc::new(RefCell::new(Shared {
        has_receiver: true,
        buffer: VecDeque::new(),
        blocked_recv: LocalWaker::new(),
        capacity,
    }));

    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,
    capacity: usize,
}

/// The transmission end of a channel.
///
/// This is created by the `channel` function.
#[derive(Debug)]
pub struct Sender<T> {
    shared: Rc<RefCell<Shared<T>>>,
}

impl<T> Unpin for Sender<T> {}

impl<T> Sender<T> {
    /// Sends the provided message along this channel.
    pub fn send(&self, item: T) -> Result<(), SendError<T>> {
        let mut shared = self.shared.borrow_mut();

        if !shared.has_receiver {
            // receiver was dropped
            return Err(SendError(item, false));
        };

        if shared.buffer.len() == shared.capacity {
            return Err(SendError(item, true));
        }

        shared.buffer.push_back(item);
        let waker = shared.blocked_recv.take();
        drop(shared);
        if let Some(waker) = waker {
            waker.wake();
        }

        Ok(())
    }

    /// Closes the sender half.
    ///
    /// This prevents any further messages from being sent on the channel, by any sender, while
    /// still enabling the receiver to drain messages that are already buffered.
    pub fn close(&mut self) {
        let mut shared = self.shared.borrow_mut();
        shared.has_receiver = false;
        let waker = shared.blocked_recv.take();
        drop(shared);
        if let Some(waker) = waker {
            waker.wake();
        }
    }
}

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>> {
        Poll::Ready(Ok(()))
    }
}

impl<T> Drop for Sender<T> {
    fn drop(&mut self) {
        let count = Rc::strong_count(&self.shared);
        let mut shared = self.shared.borrow_mut();

        // check is last sender is about to drop
        if shared.has_receiver && count == 2 {
            // The dropping sender still owns its Rc during an inline wake.
            shared.has_receiver = false;
            // Wake up receiver as its stream has ended
            let waker = shared.blocked_recv.take();
            drop(shared);
            if let Some(waker) = waker {
                waker.wake();
            }
        }
    }
}

/// The receiving end of a channel which implements the `Stream` trait.
///
/// This is created by the [`channel`] function.
#[derive(Debug)]
pub struct Receiver<T> {
    shared: Rc<RefCell<Shared<T>>>,
}

impl<T> Receiver<T> {
    /// Receive the next value.
    ///
    /// Returns `None` if the channel is empty and has been [closed](Sender::close) explicitly or
    /// when all senders have been dropped and, therefore, no more values can ever be sent though
    /// this channel.
    pub async fn recv(&mut self) -> Option<T> {
        let mut this = Pin::new(self);
        poll_fn(|cx| this.as_mut().poll_next(cx)).await
    }

    /// Create an associated [Sender].
    pub fn sender(&self) -> Sender<T> {
        Sender {
            shared: self.shared.clone(),
        }
    }
}

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>> {
        let mut shared = self.shared.borrow_mut();

        if !shared.has_receiver || Rc::strong_count(&self.shared) == 1 {
            // All senders have been dropped, so drain the buffer and end the stream.
            return Poll::Ready(shared.buffer.pop_front());
        }

        if let Some(msg) = shared.buffer.pop_front() {
            Poll::Ready(Some(msg))
        } else {
            // Registering can clone/drop wakers, which may reenter the queue.
            drop(shared);
            let blocked_recv = LocalWaker::new();
            blocked_recv.register(cx.waker());
            let previous = {
                let mut shared = self.shared.borrow_mut();
                std::mem::replace(&mut shared.blocked_recv, blocked_recv)
            };
            drop(previous);

            // Clone/drop callbacks may have delivered a value or closed the
            // channel before registration finished. Do not miss that change.
            let mut shared = self.shared.borrow_mut();
            if !shared.has_receiver || Rc::strong_count(&self.shared) == 1 {
                Poll::Ready(shared.buffer.pop_front())
            } else if let Some(msg) = shared.buffer.pop_front() {
                Poll::Ready(Some(msg))
            } else {
                Poll::Pending
            }
        }
    }
}

impl<T> Drop for Receiver<T> {
    fn drop(&mut self) {
        let mut shared = self.shared.borrow_mut();
        shared.has_receiver = false;
        let buffer = std::mem::take(&mut shared.buffer);
        let waker = shared.blocked_recv.take();
        drop(shared);
        drop(buffer);
        drop(waker);
    }
}

/// Error returned when the buffer is full or the receiver is dropped or closed.
///
/// Allows access to message that failed to send with [`into_inner`](Self::into_inner).
pub struct SendError<T>(pub T, bool);

impl<T> SendError<T> {
    /// Whether the buffer is full rather than closed.
    pub fn is_full(&self) -> bool {
        self.1
    }

    /// Returns the message that was attempted to be sent but failed.
    pub fn into_inner(self) -> T {
        self.0
    }
}

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 {
        if self.is_full() {
            write!(fmt, "send failed because buffer is full")
        } else {
            write!(fmt, "send failed because receiver is gone")
        }
    }
}

impl<T> Error for SendError<T> {}

#[cfg(test)]
mod tests {
    use futures::{StreamExt as _, future::lazy};

    use super::*;

    #[crate::test]
    async fn test_mpsc() {
        let (tx, mut rx) = channel();
        tx.send("test").unwrap();
        assert_eq!(rx.next().await.unwrap(), "test");

        let tx2 = tx.clone();
        tx2.send("test2").unwrap();
        assert_eq!(rx.next().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);
        assert_eq!(rx.next().await, None);

        let (tx, rx) = channel();
        tx.send("test").unwrap();
        drop(rx);
        assert!(tx.send("test").is_err());

        let (mut tx, _) = channel();
        let tx2 = tx.clone();
        tx.close();
        assert!(tx.send("test").is_err());
        assert!(tx2.send("test").is_err());
    }

    #[crate::test]
    async fn test_recv() {
        let (tx, mut rx) = channel();
        tx.send("test").unwrap();
        assert_eq!(rx.recv().await.unwrap(), "test");
        drop(tx);

        let (tx, mut rx) = channel();
        tx.send("test").unwrap();
        assert_eq!(rx.recv().await.unwrap(), "test");
        drop(tx);
        assert!(rx.recv().await.is_none());
    }

    #[crate::test]
    async fn test_bounded() {
        let (tx, mut rx) = bounded(1);
        let mut tx2 = tx.clone();
        tx.send(1).unwrap();
        let error = tx2.send(2).unwrap_err();
        assert!(error.is_full());
        assert_eq!(error.into_inner(), 2);
        assert_eq!(rx.recv().await, Some(1));
        tx2.send(2).unwrap();
        tx2.close();
        assert!(!tx.send(3).unwrap_err().is_full());
        assert_eq!(rx.recv().await, Some(2));
        assert_eq!(rx.recv().await, None);

        let (tx, rx) = bounded(1);
        tx.send(1).unwrap();
        drop(rx);
        assert!(!tx.send(2).unwrap_err().is_full());
    }

    thread_local! {
        static ON_CLONE: RefCell<Option<Box<dyn FnOnce()>>> = const { RefCell::new(None) };
        static ON_DROP: RefCell<Option<Box<dyn FnOnce()>>> = const { RefCell::new(None) };
        static ON_WAKE: RefCell<Option<Box<dyn FnOnce()>>> = const { RefCell::new(None) };
    }

    fn callback_waker() -> std::task::Waker {
        use std::task::{RawWaker, RawWakerVTable, Waker};
        unsafe fn clone(_: *const ()) -> RawWaker {
            let callback = ON_CLONE.with(|slot| slot.borrow_mut().take());
            if let Some(callback) = callback {
                callback();
            }
            RawWaker::new(std::ptr::null(), &VTABLE)
        }
        unsafe fn drop(_: *const ()) {
            let callback = ON_DROP.with(|slot| slot.borrow_mut().take());
            if let Some(callback) = callback {
                callback();
            }
        }
        unsafe fn wake(_: *const ()) {
            let callback = ON_WAKE.with(|slot| slot.borrow_mut().take());
            if let Some(callback) = callback {
                callback();
            }
        }
        static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);
        // No owned raw data; callbacks use only the calling thread's storage.
        unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTABLE)) }
    }

    #[test]
    fn waker_clone_and_drop_can_reenter() {
        for on_clone in [true, false] {
            let (tx, mut rx) = bounded(1);
            let waker = callback_waker();
            let mut cx = Context::from_waker(&waker);
            assert!(Pin::new(&mut rx).poll_next(&mut cx).is_pending());
            let callback: Box<dyn FnOnce()> = Box::new(move || tx.send(7).unwrap());
            if on_clone {
                ON_CLONE.with(|slot| *slot.borrow_mut() = Some(callback));
            } else {
                ON_DROP.with(|slot| *slot.borrow_mut() = Some(callback));
            }
            assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(Some(7)));
            assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(None));
        }
    }

    #[test]
    fn last_sender_drop_can_wake_and_poll_inline() {
        let (tx, mut rx) = channel::<()>();
        let waker = callback_waker();
        assert!(
            Pin::new(&mut rx)
                .poll_next(&mut Context::from_waker(&waker))
                .is_pending()
        );
        ON_WAKE.with(|slot| {
            *slot.borrow_mut() = Some(Box::new(move || {
                let mut cx = Context::from_waker(std::task::Waker::noop());
                assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(None));
            }))
        });
        drop(tx);
        ON_WAKE.with(|slot| assert!(slot.borrow().is_none()));
    }

    #[test]
    fn queued_values_drop_outside_the_borrow() {
        struct Value(Sender<Value>);
        impl Drop for Value {
            fn drop(&mut self) {
                drop(self.0.clone());
            }
        }
        let (tx, rx) = bounded(1);
        tx.send(Value(tx.clone())).unwrap();
        drop(rx);
        drop(tx);
    }
}