2021-04-05 12:49:00 -05:00
|
|
|
//! Bookkeeping to make sure only one long-running operation is being executed
|
|
|
|
//! at a time.
|
2021-01-10 09:02:02 -06:00
|
|
|
|
2021-07-18 03:29:22 -05:00
|
|
|
pub(crate) struct OpQueue<Output> {
|
|
|
|
op_requested: bool,
|
2021-01-10 09:02:02 -06:00
|
|
|
op_in_progress: bool,
|
2021-04-05 12:49:00 -05:00
|
|
|
last_op_result: Output,
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
|
|
|
|
2021-07-18 03:29:22 -05:00
|
|
|
impl<Output: Default> Default for OpQueue<Output> {
|
2021-01-28 09:33:02 -06:00
|
|
|
fn default() -> Self {
|
2021-07-18 03:29:22 -05:00
|
|
|
Self { op_requested: false, op_in_progress: false, last_op_result: Default::default() }
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2021-01-28 09:33:02 -06:00
|
|
|
}
|
|
|
|
|
2021-07-18 03:29:22 -05:00
|
|
|
impl<Output> OpQueue<Output> {
|
|
|
|
pub(crate) fn request_op(&mut self) {
|
|
|
|
self.op_requested = true;
|
2021-01-28 09:33:02 -06:00
|
|
|
}
|
2021-07-18 03:29:22 -05:00
|
|
|
pub(crate) fn should_start_op(&mut self) -> bool {
|
2021-01-28 09:33:02 -06:00
|
|
|
if self.op_in_progress {
|
2021-07-18 03:29:22 -05:00
|
|
|
return false;
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2021-07-18 03:29:22 -05:00
|
|
|
self.op_in_progress = self.op_requested;
|
|
|
|
self.op_requested = false;
|
|
|
|
self.op_in_progress
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2021-04-05 12:49:00 -05:00
|
|
|
pub(crate) fn op_completed(&mut self, result: Output) {
|
2021-01-10 09:02:02 -06:00
|
|
|
assert!(self.op_in_progress);
|
|
|
|
self.op_in_progress = false;
|
2021-04-05 12:49:00 -05:00
|
|
|
self.last_op_result = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn last_op_result(&self) -> &Output {
|
|
|
|
&self.last_op_result
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2021-04-06 06:16:35 -05:00
|
|
|
pub(crate) fn op_in_progress(&self) -> bool {
|
|
|
|
self.op_in_progress
|
|
|
|
}
|
|
|
|
pub(crate) fn op_requested(&self) -> bool {
|
2021-07-18 03:29:22 -05:00
|
|
|
self.op_requested
|
2021-04-06 06:16:35 -05:00
|
|
|
}
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|