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
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use futures::FutureExt;
use tokio::task;

use super::{Task, TaskId};

#[derive(Debug)]
pub struct JoinError(task::JoinError);

#[derive(Debug)]
pub struct JoinHandle<T> {
    task: Task,
    handle: task::JoinHandle<T>,
}

impl<T> JoinHandle<T> {
    pub(crate) fn new(handle: task::JoinHandle<T>) -> Self {
        Self {
            task: Task(TaskId),
            handle,
        }
    }

    pub fn task(&self) -> &Task {
        &self.task
    }
}

impl<T> Future for JoinHandle<T> {
    type Output = Result<T, JoinError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.handle.poll_unpin(cx).map_err(JoinError)
    }
}