use crate::units::time::Microsecond;
#[derive(Debug, Clone, Copy)]
pub enum NotifierUpdateType {
Periodic {
period: Microsecond,
skip_missed: bool,
},
OneShot {
trigger_time: Microsecond,
},
RelativeOneShot {
trigger_offset_time: Microsecond,
},
}
pub trait Notifier: Send + Sync {
fn get_name(&self) -> &str;
fn update_alarm(&mut self, update: NotifierUpdateType);
fn cancel_alarm(&mut self);
fn wait_for_alarm(&mut self) -> Microsecond;
}
type NotifierHandle = Box<dyn Notifier>;
pub trait NotifierDriver: 'static {
fn new_notifier() -> impl Notifier;
}
#[derive(Debug, Clone, Copy)]
pub struct NotifierVTable {
pub(crate) new_notifier: fn() -> NotifierHandle,
}
impl NotifierVTable {
pub(crate) fn from_driver<T: NotifierDriver>() -> Self {
assert!(
std::mem::size_of::<T>() == 0,
"Notifier Driver must be zero sized"
);
Self {
new_notifier: || Box::new(T::new_notifier()),
}
}
#[must_use]
pub fn new_notifier(&self) -> NotifierHandle {
(self.new_notifier)()
}
}