pub trait SimState {
// Required methods
fn get_effect(&self) -> Effect;
fn set_effect(&mut self, effect: Effect);
fn should_log(&self) -> bool;
}Expand description
Data structures implementing this trait can be yielded from the coroutine
associated with a Process. This allows attaching application-specific data
to Effects. This data is then carried arround by the Simulation, passed
into user callbacks for context or simply logged for later.
As a simple example, implementing SimState for a type (as shown for ItemState below) allows users to track item stages.
A process can then yield ItemState instead of Effect types:
#![feature (coroutines, coroutine_trait)]
use desim::{Effect, SimState, Simulation};
// enum used as part of state logged during simulation
#[derive(Clone)]
enum StageType {
FirstPass,
SecondPass,
}
// structure yielded from processes of the simulation
#[derive(Clone)]
struct ItemState {
stage: StageType,
effect: Effect,
log: bool,
}
impl SimState for ItemState {
fn get_effect(&self) -> Effect { self.effect }
fn set_effect(&mut self, e: Effect) { self.effect = e; }
fn should_log(&self) -> bool { self.log }
}
let mut sim = Simulation::new();
sim.create_process(Box::new(move |_| {
yield ItemState { stage: StageType::FirstPass,
effect: Effect::TimeOut(10.0),
log: true,
};
}));Calling sim.processed_steps() then returns a vector of (Event, ItemState)
pairs, one for each yielded value where should_log() returned true.
For a full example, see examples/monitoring-state.rs