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
use std::fmt;
use std::mem;
use super::job_queue::JobState;
use crate::util::CargoResult;
pub struct Job {
work: Work,
fresh: Freshness,
}
pub struct Work {
inner: Box<dyn FnOnce(&JobState<'_, '_>) -> CargoResult<()> + Send>,
}
impl Work {
pub fn new<F>(f: F) -> Work
where
F: FnOnce(&JobState<'_, '_>) -> CargoResult<()> + Send + 'static,
{
Work { inner: Box::new(f) }
}
pub fn noop() -> Work {
Work::new(|_| Ok(()))
}
pub fn call(self, tx: &JobState<'_, '_>) -> CargoResult<()> {
(self.inner)(tx)
}
pub fn then(self, next: Work) -> Work {
Work::new(move |state| {
self.call(state)?;
next.call(state)
})
}
}
impl Job {
pub fn new_fresh() -> Job {
Job {
work: Work::noop(),
fresh: Freshness::Fresh,
}
}
pub fn new_dirty(work: Work) -> Job {
Job {
work,
fresh: Freshness::Dirty,
}
}
pub fn run(self, state: &JobState<'_, '_>) -> CargoResult<()> {
self.work.call(state)
}
pub fn freshness(&self) -> Freshness {
self.fresh
}
pub fn before(&mut self, next: Work) {
let prev = mem::replace(&mut self.work, Work::noop());
self.work = next.then(prev);
}
}
impl fmt::Debug for Job {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Job {{ ... }}")
}
}
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum Freshness {
Fresh,
Dirty,
}