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
use crate::data::DataValue;
use crate::task_tree::{TaskTree, TASK_TREE};
use crate::uniq_id::UniqID;
use anyhow::Result;
use std::future::Future;
use std::sync::Arc;
pub type MarkDoneOnDrop = bool;
#[derive(Clone)]
pub struct Task(pub(crate) Arc<TaskData>);
pub(crate) struct TaskData {
pub(crate) id: UniqID,
pub(crate) task_tree: TaskTree,
pub(crate) mark_done_on_drop: MarkDoneOnDrop,
}
impl Task {
pub fn create_new(name: &str) -> Self {
let id = TASK_TREE.create_task_internal(name, None);
Self(Arc::new(TaskData {
id,
task_tree: TASK_TREE.clone(),
mark_done_on_drop: true,
}))
}
pub fn create(&self, name: &str) -> Self {
let id = self.0.task_tree.create_task_internal(name, Some(self.0.id));
Self(Arc::new(TaskData {
id,
task_tree: self.0.task_tree.clone(),
mark_done_on_drop: true,
}))
}
pub async fn spawn_new<F, FT, T>(name: &str, f: F) -> Result<T>
where
F: FnOnce(Task) -> FT,
FT: Future<Output = Result<T>> + Send + 'static,
T: Send + 'static,
{
TASK_TREE.spawn(name, f, None).await
}
pub async fn spawn<F, FT, T>(&self, name: &str, f: F) -> Result<T>
where
F: FnOnce(Task) -> FT,
FT: Future<Output = Result<T>> + Send,
T: Send,
{
self.0.task_tree.spawn(name, f, Some(self.0.id)).await
}
pub fn spawn_sync<F, T>(&self, name: &str, f: F) -> Result<T>
where
F: FnOnce(Task) -> Result<T>,
T: Send + 'static,
{
self.0.task_tree.spawn_sync(name, f, Some(self.0.id))
}
pub fn data<D: Into<DataValue>>(&self, name: &str, data: D) {
self.0.task_tree.add_data(self.0.id, name, data);
}
pub fn data_transitive<D: Into<DataValue>>(&self, name: &str, data: D) {
self.0.task_tree.add_data_transitive(self.0.id, name, data);
}
}
impl Drop for TaskData {
fn drop(&mut self) {
if self.mark_done_on_drop {
self.task_tree.mark_done(self.id, None);
}
}
}