Trait asynchronix::model::Model
source · pub trait Model: Sized + Send + 'static {
// Provided method
fn init(
self,
scheduler: &Scheduler<Self>
) -> Pin<Box<dyn Future<Output = InitializedModel<Self>> + Send + '_>> { ... }
}Expand description
Trait to be implemented by all models.
This trait enables models to perform specific actions in the
Model::init() method only once all models have been connected and
migrated to the simulation bench, but before the simulation actually starts.
A common use for init is to send messages to connected models at the
beginning of the simulation.
The init function converts the model to the opaque InitializedModel type
to prevent an already initialized model from being added to the simulation
bench.
Provided Methods§
sourcefn init(
self,
scheduler: &Scheduler<Self>
) -> Pin<Box<dyn Future<Output = InitializedModel<Self>> + Send + '_>>
fn init( self, scheduler: &Scheduler<Self> ) -> Pin<Box<dyn Future<Output = InitializedModel<Self>> + Send + '_>>
Performs asynchronous model initialization.
This asynchronous method is executed exactly once for all models of the
simulation when the
SimInit::init() method is called.
The default implementation simply converts the model to an
InitializedModel without any side effect.
Note: it is currently necessary to box the returned future; this
limitation will be lifted once Rust supports async methods in traits.
Examples
use std::future::Future;
use std::pin::Pin;
use asynchronix::model::{InitializedModel, Model};
use asynchronix::time::Scheduler;
pub struct MyModel {
// ...
}
impl Model for MyModel {
fn init(
self,
scheduler: &Scheduler<Self>
) -> Pin<Box<dyn Future<Output = InitializedModel<Self>> + Send + '_>>{
Box::pin(async move {
println!("...initialization...");
self.into()
})
}
}