1
0
forked from ROMEO/nexosim
nexosim/nexosim-util/src/helper_models.rs
2025-01-28 11:08:17 +01:00

36 lines
833 B
Rust

//! Helper models.
//!
//! This module contains helper models useful for simulation bench assembly.
//!
use std::time::Duration;
use nexosim::model::{Context, InitializedModel, Model};
/// A ticker model.
///
/// This model self-schedules at the specified period, which can be used to keep
/// the simulation alive.
pub struct Ticker {
/// Tick period.
tick: Duration,
}
impl Ticker {
/// Creates a new `Ticker` with the specified self-scheduling period.
pub fn new(tick: Duration) -> Self {
Self { tick }
}
/// Self-scheduled function.
async fn tick(&mut self) {}
}
impl Model for Ticker {
async fn init(self, cx: &mut Context<Self>) -> InitializedModel<Self> {
cx.schedule_periodic_event(self.tick, self.tick, Self::tick, ())
.unwrap();
self.into()
}
}