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
use std::{fmt::Debug, sync::Arc};
use flume::Receiver;
use serde::{Deserialize, Serialize};
use crate::manager::Manager;
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
pub struct Id(pub(crate) u64);
#[derive(Debug)]
pub struct Handle<T, Key> {
pub id: Id,
pub(crate) manager: Manager<Key>,
pub(crate) receiver: Receiver<Result<T, Arc<anyhow::Error>>>,
}
impl<T, Key> Handle<T, Key>
where
T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
Key: Clone + std::hash::Hash + Eq + Send + Sync + Debug + 'static,
{
pub async fn clone(&self) -> Self {
let mut jobs = self.manager.jobs.write().await;
jobs.create_new_task_handle(self.id, self.manager.clone())
}
pub async fn receive(&self) -> Result<Result<T, Arc<anyhow::Error>>, flume::RecvError> {
self.receiver.recv_async().await
}
pub fn try_receive(&self) -> Result<Result<T, Arc<anyhow::Error>>, flume::TryRecvError> {
self.receiver.try_recv()
}
}