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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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: Arc<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,
T: Send,
{
TASK_TREE.spawn(name.into(), f, None).await
}
pub async fn spawn<F, FT, T, S: Into<String>>(&self, name: S, f: F) -> Result<T>
where
F: FnOnce(Task) -> FT,
FT: Future<Output = Result<T>> + Send,
T: Send,
{
self.0
.task_tree
.spawn(name.into(), f, Some(self.0.id))
.await
}
pub fn spawn_sync<F, T, S: Into<String>>(&self, name: S, f: F) -> Result<T>
where
F: FnOnce(Task) -> Result<T>,
T: Send,
{
self.0.task_tree.spawn_sync(name.into(), 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 get_data(&self, name: &str) -> Option<DataValue> {
self.0.task_tree.get_data(self.0.id, name)
}
pub fn data_transitive<D: Into<DataValue>>(&self, name: &str, data: D) {
self.0
.task_tree
.add_data_transitive_for_task(self.0.id, name, data);
}
pub fn progress(&self, done: i64, total: i64) {
self.0.task_tree.task_progress(self.0.id, done, total);
}
pub fn hide_error_msg(&self, msg: Option<String>) {
let msg = msg.map(Arc::new);
self.0.task_tree.hide_error_msg_for_task(self.0.id, msg);
}
pub fn attach_transitive_data_to_errors(&self, val: bool) {
self.0
.task_tree
.attach_transitive_data_to_errors_for_task(self.0.id, val);
}
}
impl Drop for TaskData {
fn drop(&mut self) {
if self.mark_done_on_drop {
self.task_tree.mark_done(self.id, None);
}
}
}