cargo/core/compiler/job_queue/
job.rs1use std::fmt;
4use std::mem;
5
6use super::JobState;
7use crate::core::compiler::fingerprint::DirtyReason;
8use crate::util::CargoResult;
9
10pub struct Job {
13 work: Work,
14 fresh: Freshness,
15}
16
17pub struct Work {
22 inner: Box<dyn FnOnce(&JobState<'_, '_>) -> CargoResult<()> + Send>,
23}
24
25impl Work {
26 pub fn new<F>(f: F) -> Work
28 where
29 F: FnOnce(&JobState<'_, '_>) -> CargoResult<()> + Send + 'static,
30 {
31 Work { inner: Box::new(f) }
32 }
33
34 pub fn noop() -> Work {
36 Work::new(|_| Ok(()))
37 }
38
39 pub fn call(self, tx: &JobState<'_, '_>) -> CargoResult<()> {
41 (self.inner)(tx)
42 }
43
44 pub fn then(self, next: Work) -> Work {
46 Work::new(move |state| {
47 self.call(state)?;
48 next.call(state)
49 })
50 }
51}
52
53impl Job {
54 pub fn new_fresh() -> Job {
56 Job {
57 work: Work::noop(),
58 fresh: Freshness::Fresh,
59 }
60 }
61
62 pub fn new_dirty(work: Work, dirty_reason: DirtyReason) -> Job {
64 Job {
65 work,
66 fresh: Freshness::Dirty(dirty_reason),
67 }
68 }
69
70 pub fn run(self, state: &JobState<'_, '_>) -> CargoResult<()> {
73 self.work.call(state)
74 }
75
76 pub fn freshness(&self) -> &Freshness {
80 &self.fresh
81 }
82
83 pub fn before(&mut self, next: Work) {
85 let prev = mem::replace(&mut self.work, Work::noop());
86 self.work = next.then(prev);
87 }
88
89 pub fn after(&mut self, next: Work) {
91 let prev = mem::replace(&mut self.work, Work::noop());
92 self.work = prev.then(next);
93 }
94}
95
96impl fmt::Debug for Job {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 write!(f, "Job {{ ... }}")
99 }
100}
101
102#[derive(Debug, Clone)]
107pub enum Freshness {
108 Fresh,
109 Dirty(DirtyReason),
110}
111
112impl Freshness {
113 pub fn is_dirty(&self) -> bool {
114 matches!(self, Freshness::Dirty(_))
115 }
116
117 pub fn is_fresh(&self) -> bool {
118 matches!(self, Freshness::Fresh)
119 }
120}