cargo/core/compiler/job_queue/
job.rs
1use 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
90impl fmt::Debug for Job {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 write!(f, "Job {{ ... }}")
93 }
94}
95
96#[derive(Debug, Clone)]
101pub enum Freshness {
102 Fresh,
103 Dirty(DirtyReason),
104}
105
106impl Freshness {
107 pub fn is_dirty(&self) -> bool {
108 matches!(self, Freshness::Dirty(_))
109 }
110
111 pub fn is_fresh(&self) -> bool {
112 matches!(self, Freshness::Fresh)
113 }
114}