1
use std::{fmt::Debug, sync::Arc};
2

            
3
use tokio::sync::oneshot;
4

            
5
/// he `Id` of an executing task.
6
339659
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
7
pub struct Id(pub(crate) u64);
8

            
9
/// References a background task.
10
#[derive(Debug)]
11
pub struct Handle<T, E> {
12
    /// The task's id.
13
    pub id: Id,
14

            
15
    pub(crate) receiver: oneshot::Receiver<Result<T, Arc<E>>>,
16
}
17

            
18
impl<T, E> Handle<T, E>
19
where
20
    T: Send + Sync + 'static,
21
    E: Send + Sync + 'static,
22
{
23
    /// Waits for the job to complete and returns the result.
24
    ///
25
    /// # Errors
26
    ///
27
    /// Returns an error if the job is cancelled.
28
161241
    pub async fn receive(
29
161241
        self,
30
161241
    ) -> Result<Result<T, Arc<E>>, tokio::sync::oneshot::error::RecvError> {
31
161241
        self.receiver.await
32
161241
    }
33

            
34
    // /// Tries to receive the status of the job. If available, it is returned.
35
    // /// This function will not block.
36
    // ///
37
    // /// # Errors
38
    // ///
39
    // /// Returns an error if the job isn't complete.
40
    // ///
41
    // /// * [`TryRecvError::Disconnected`](flume::TryRecvError::Disconnected): The job has been cancelled.
42
    // /// * [`TryRecvError::Empty`](flume::TryRecvError::Empty): The job has not completed yet.
43
    #[cfg(test)]
44
1
    pub fn try_receive(
45
1
        &mut self,
46
1
    ) -> Result<Result<T, Arc<E>>, tokio::sync::oneshot::error::TryRecvError> {
47
1
        self.receiver.try_recv()
48
1
    }
49
}