pub trait Model:
Sized
+ Send
+ 'static {
// Provided method
fn init(
self,
_: &mut Context<Self>,
) -> impl Future<Output = InitializedModel<Self>> + Send { ... }
}
Expand description
Trait to be implemented by simulation models.
This trait enables models to perform specific actions during initialization.
The Model::init
method is run 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,
_: &mut Context<Self>,
) -> impl Future<Output = InitializedModel<Self>> + Send
fn init( self, _: &mut Context<Self>, ) -> impl 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.
§Examples
use std::future::Future;
use std::pin::Pin;
use nexosim::model::{Context, InitializedModel, Model};
pub struct MyModel {
// ...
}
impl Model for MyModel {
async fn init(
self,
cx: &mut Context<Self>
) -> InitializedModel<Self> {
println!("...initialization...");
self.into()
}
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.