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
use crate::{local_future::LocalRes, state::State};

use std::{
    fmt,
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

/// Wait for a spawned task to complete or cancel it.
pub struct JoinHandle<R> {
    id: u64,
    receiver_fut: Pin<Box<dyn Future<Output = Result<R, flume::RecvError>>>>,
    state: State,
}

impl<R: 'static> JoinHandle<R> {
    pub(crate) fn new(id: u64, receiver: flume::Receiver<R>, state: State) -> Self {
        Self {
            id,
            receiver_fut: Box::pin(receiver.into_recv_async()),
            state,
        }
    }

    /// Cancel a spawned task, returning its result if it was finished
    pub fn cancel(mut self) -> Option<R> {
        let mut cx = Context::from_waker(&*crate::DUMMY_WAKER);
        self.state.cancel(self.id);
        match self.receiver_fut.as_mut().poll(&mut cx) {
            Poll::Ready(res) => res.ok(),
            Poll::Pending => None,
        }
    }
}

impl<R> Future for JoinHandle<R> {
    type Output = R;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.receiver_fut
            .as_mut()
            .poll(cx)
            .map(|res| res.expect("inner channel isn't expected to fail"))
    }
}

unsafe impl<R: Send> Send for JoinHandle<R> {}

impl<R> fmt::Debug for JoinHandle<R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JoinHandle").field("id", &self.id).finish()
    }
}

/// Wait for a spawned local task to complete or cancel it.
pub struct LocalJoinHandle<R>(pub(crate) JoinHandle<LocalRes<R>>);

impl<R: 'static> LocalJoinHandle<R> {
    /// Cancel a spawned local task, returning its result if it was finished
    pub fn cancel(self) -> Option<R> {
        self.0.cancel().map(|res| res.0)
    }
}

impl<R> Future for LocalJoinHandle<R> {
    type Output = R;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.0).poll(cx).map(|res| res.0)
    }
}

impl<R> fmt::Debug for LocalJoinHandle<R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LocalJoinHandle")
            .field("id", &self.0.id)
            .finish()
    }
}