Merge pull request #27 from asynchronics/feature-auto-action-key

Add AutoActionKey
This commit is contained in:
Serge Barral authored and GitHub committed 2024-06-26 13:45:54 +02:00
commit 7e5f623ac5
2 files changed
+24 -1

No files matched your search

+1 -1
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;
+23
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 {