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
use super::*;

use core::task::{Context, Poll};

#[derive(Debug)]
pub struct MustJoinHandle<T> {
    join_handle: Option<LowLevelJoinHandle<T>>,
    completed: bool,
}

impl<T> MustJoinHandle<T> {
    pub fn new(join_handle: LowLevelJoinHandle<T>) -> Self {
        Self {
            join_handle: Some(join_handle),
            completed: false,
        }
    }

    pub fn detach(mut self) {
        cfg_if! {
            if #[cfg(feature="rt-async-std")] {
                self.join_handle = None;
            } else if #[cfg(feature="rt-tokio")] {
                self.join_handle = None;
            } else if #[cfg(target_arch = "wasm32")] {
                if let Some(jh) = self.join_handle.take() {
                    jh.detach();
                }
            } else {
                compile_error!("needs executor implementation")
            }
        }
        self.completed = true;
    }

    #[allow(unused_mut)]
    pub async fn abort(mut self) {
        if !self.completed {
            cfg_if! {
                if #[cfg(feature="rt-async-std")] {
                    if let Some(jh) = self.join_handle.take() {
                        jh.cancel().await;
                        self.completed = true;
                    }
                } else if #[cfg(feature="rt-tokio")] {
                    if let Some(jh) = self.join_handle.take() {
                        jh.abort();
                        let _ = jh.await;
                        self.completed = true;
                    }
                } else if #[cfg(target_arch = "wasm32")] {
                    drop(self.join_handle.take());
                    self.completed = true;
                } else {
                    compile_error!("needs executor implementation")
                }

            }
        }
    }
}

impl<T> Drop for MustJoinHandle<T> {
    fn drop(&mut self) {
        // panic if we haven't completed
        if !self.completed {
            panic!("MustJoinHandle was not completed upon drop. Add cooperative cancellation where appropriate to ensure this is completed before drop.")
        }
    }
}

impl<T: 'static> Future for MustJoinHandle<T> {
    type Output = T;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match Pin::new(self.join_handle.as_mut().unwrap()).poll(cx) {
            Poll::Ready(t) => {
                if self.completed {
                    panic!("should not poll completed join handle");
                }
                self.completed = true;
                cfg_if! {
                    if #[cfg(feature="rt-async-std")] {
                        Poll::Ready(t)
                    } else if #[cfg(feature="rt-tokio")] {
                        match t {
                            Ok(t) => Poll::Ready(t),
                            Err(e) => {
                                if e.is_panic() {
                                    // Resume the panic on the main task
                                    std::panic::resume_unwind(e.into_panic());
                                } else {
                                    panic!("join error was not a panic, should not poll after abort");
                                }
                            }
                        }
                    } else if #[cfg(target_arch = "wasm32")] {
                        Poll::Ready(t)
                    } else {
                        compile_error!("needs executor implementation")
                    }
                }
            }
            Poll::Pending => Poll::Pending,
        }
    }
}