1
0
forked from ROMEO/nexosim

Add AutoActionKey

This commit is contained in:
Jaŭhien Piatlicki 2024-06-26 12:26:06 +02:00
parent cb7caa10e9
commit 8b015b2eba
2 changed files with 24 additions and 1 deletions

View File

@ -132,7 +132,7 @@ pub(crate) use scheduler::{
schedule_periodic_event_at_unchecked, schedule_periodic_keyed_event_at_unchecked,
KeyedOnceAction, KeyedPeriodicAction, OnceAction, PeriodicAction, SchedulerQueue,
};
pub use scheduler::{Action, ActionKey, Deadline, SchedulingError};
pub use scheduler::{Action, ActionKey, AutoActionKey, Deadline, SchedulingError};
pub use sim_init::SimInit;
use std::error::Error;

View File

@ -54,6 +54,22 @@ impl Deadline for MonotonicTime {
}
}
/// Managed handle to a scheduled action.
///
/// An `AutoActionKey` is a managed handle to a scheduled action that cancels
/// action on drop.
#[derive(Debug)]
#[must_use = "managed action key shall be used"]
pub struct AutoActionKey {
is_cancelled: Arc<AtomicBool>,
}
impl Drop for AutoActionKey {
fn drop(&mut self) {
self.is_cancelled.store(true, Ordering::Relaxed);
}
}
/// Handle to a scheduled action.
///
/// An `ActionKey` can be used to cancel a scheduled action.
@ -80,6 +96,13 @@ impl ActionKey {
pub fn cancel(self) {
self.is_cancelled.store(true, Ordering::Relaxed);
}
/// Converts action key to a managed key.
pub fn into_auto(self) -> AutoActionKey {
AutoActionKey {
is_cancelled: self.is_cancelled,
}
}
}
impl PartialEq for ActionKey {