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
use crate::{
    fib::{self, Fiber},
    sync::spsc::oneshot::{channel, Canceled, Receiver},
    thr::prelude::*,
};
use core::{
    future::Future,
    intrinsics::unreachable,
    pin::Pin,
    task::{Context, Poll},
};

/// A future that resolves on completion of the fiber from another thread.
///
/// Dropping or closing this future will remove the fiber on a next thread
/// invocation without resuming it.
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct FiberFuture<T> {
    rx: Receiver<T>,
}

#[marker]
pub trait YieldNone: Send + 'static {}

impl YieldNone for () {}
impl YieldNone for ! {}

impl<T> FiberFuture<T> {
    /// Gracefully close this future.
    ///
    /// The fiber will be removed on a next thread invocation without resuming.
    #[inline]
    pub fn close(&mut self) {
        self.rx.close()
    }
}

impl<T> Future for FiberFuture<T> {
    type Output = T;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
        let rx = unsafe { self.map_unchecked_mut(|x| &mut x.rx) };
        rx.poll(cx).map(|value| match value {
            Ok(value) => value,
            Err(Canceled) => unsafe { unreachable() },
        })
    }
}

/// Extends [`ThrToken`](crate::thr::ThrToken) types with `add_future` method.
pub trait ThrFiberFuture: ThrToken {
    /// Adds the fiber `fib` to the fiber chain and returns a future, which
    /// resolves on completion of the fiber.
    #[inline]
    fn add_future<F, Y, T>(self, fib: F) -> FiberFuture<T>
    where
        F: Fiber<Input = (), Yield = Y, Return = T>,
        Y: YieldNone,
        F: Send + 'static,
        T: Send + 'static,
    {
        FiberFuture {
            rx: add_rx(self, fib),
        }
    }
}

#[inline]
fn add_rx<H, F, Y, T>(thr: H, mut fib: F) -> Receiver<T>
where
    H: ThrToken,
    F: Fiber<Input = (), Yield = Y, Return = T>,
    Y: YieldNone,
    F: Send + 'static,
    T: Send + 'static,
{
    let (tx, rx) = channel();
    thr.add(move || {
        loop {
            if tx.is_canceled() {
                break;
            }
            match unsafe { Pin::new_unchecked(&mut fib) }.resume(()) {
                fib::Yielded(_) => {}
                fib::Complete(complete) => {
                    drop(tx.send(complete));
                    break;
                }
            }
            yield;
        }
    });
    rx
}

impl<T: ThrToken> ThrFiberFuture for T {}