async_std/task/
join_handle.rs1use std::future::Future;
2use std::pin::Pin;
3
4use crate::task::{Context, Poll, Task};
5
6#[derive(Debug)]
15pub struct JoinHandle<T> {
16 handle: Option<InnerHandle<T>>,
17 task: Task,
18}
19
20#[cfg(not(target_os = "unknown"))]
21type InnerHandle<T> = async_global_executor::Task<T>;
22#[cfg(target_arch = "wasm32")]
23type InnerHandle<T> = futures_channel::oneshot::Receiver<T>;
24
25impl<T> JoinHandle<T> {
26 pub(crate) fn new(inner: InnerHandle<T>, task: Task) -> JoinHandle<T> {
28 JoinHandle {
29 handle: Some(inner),
30 task,
31 }
32 }
33
34 pub fn task(&self) -> &Task {
50 &self.task
51 }
52
53 #[cfg(not(target_os = "unknown"))]
55 pub async fn cancel(mut self) -> Option<T> {
56 let handle = self.handle.take().unwrap();
57 handle.cancel().await
58 }
59
60 #[cfg(target_arch = "wasm32")]
62 pub async fn cancel(mut self) -> Option<T> {
63 let mut handle = self.handle.take().unwrap();
64 handle.close();
65 handle.await.ok()
66 }
67}
68
69#[cfg(not(target_os = "unknown"))]
70impl<T> Drop for JoinHandle<T> {
71 fn drop(&mut self) {
72 if let Some(handle) = self.handle.take() {
73 handle.detach();
74 }
75 }
76}
77
78impl<T> Future for JoinHandle<T> {
79 type Output = T;
80
81 #[cfg(not(target_os = "unknown"))]
82 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
83 Pin::new(&mut self.handle.as_mut().unwrap()).poll(cx)
84 }
85
86 #[cfg(target_arch = "wasm32")]
87 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
88 match Pin::new(&mut self.handle.as_mut().unwrap()).poll(cx) {
89 Poll::Ready(Ok(t)) => Poll::Ready(t),
90 Poll::Ready(Err(_)) => unreachable!("channel must not be canceled"),
91 Poll::Pending => Poll::Pending,
92 }
93 }
94}