Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.3 KiB
Rust
Raw Normal View History

//! Bookkeeping to make sure only one long-running operation is being executed
//! at a time.
pub(crate) type Cause = String;
2023-03-26 08:39:28 +02:00
pub(crate) struct OpQueue<Args = (), Output = ()> {
op_requested: Option<(Cause, Args)>,
op_in_progress: bool,
last_op_result: Output,
}
2023-03-26 08:39:28 +02:00
impl<Args, Output: Default> Default for OpQueue<Args, Output> {
2021-01-28 23:33:02 +08:00
fn default() -> Self {
Self { op_requested: None, op_in_progress: false, last_op_result: Default::default() }
}
2021-01-28 23:33:02 +08:00
}
2023-03-26 08:39:28 +02:00
impl<Args, Output> OpQueue<Args, Output> {
pub(crate) fn request_op(&mut self, reason: Cause, args: Args) {
self.op_requested = Some((reason, args));
2021-01-28 23:33:02 +08:00
}
2023-03-26 08:39:28 +02:00
pub(crate) fn should_start_op(&mut self) -> Option<(Cause, Args)> {
2021-01-28 23:33:02 +08:00
if self.op_in_progress {
return None;
}
self.op_in_progress = self.op_requested.is_some();
self.op_requested.take()
}
pub(crate) fn op_completed(&mut self, result: Output) {
assert!(self.op_in_progress);
self.op_in_progress = false;
self.last_op_result = result;
}
pub(crate) fn last_op_result(&self) -> &Output {
&self.last_op_result
}
2021-04-06 14:16:35 +03:00
pub(crate) fn op_in_progress(&self) -> bool {
self.op_in_progress
}
pub(crate) fn op_requested(&self) -> bool {
self.op_requested.is_some()
2021-04-06 14:16:35 +03:00
}
}