pub trait Simulation: SimulationState {
type LoadData;
type StateLoadingError;
// Required methods
fn reload(
&mut self,
data: Self::LoadData,
) -> Result<(), Self::StateLoadingError>;
unsafe fn call(&mut self, event: Self::Event);
unsafe fn revert(&mut self, event: Self::Event);
// Provided methods
fn try_call(&mut self, event: Self::Event) -> bool { ... }
fn try_revert(&mut self, event: Self::Event) -> bool { ... }
fn prepare_call(&mut self) -> CallState<'_, Self, Call> { ... }
fn prepare_revert(&mut self) -> CallState<'_, Self, Revert> { ... }
}Expand description
The Simulation trait extends SimulationState by providing mutable access to modify the simulation state via events.
Required Associated Types§
Sourcetype StateLoadingError
type StateLoadingError
The error type returned when loading the simulation state fails.
Required Methods§
Sourcefn reload(
&mut self,
data: Self::LoadData,
) -> Result<(), Self::StateLoadingError>
fn reload( &mut self, data: Self::LoadData, ) -> Result<(), Self::StateLoadingError>
Reloads the simulation state from the provided data.
§Errors
Returns an error if a valid state cannot be loaded from data.
Provided Methods§
Sourcefn try_call(&mut self, event: Self::Event) -> bool
fn try_call(&mut self, event: Self::Event) -> bool
Tries to call the provided event and returns if it was successful.
Examples found in repository?
examples/text_adventure.rs (line 161)
134fn main() {
135 let adventure = Adventure {
136 rooms: vec![
137 "You stand at a crossroads.".into(),
138 "A dark cave yawns before you.".into(),
139 "Sunlight breaks over a quiet meadow.".into(),
140 ],
141 passages: vec![
142 passage("Enter the cave", 0, 1),
143 passage("Walk to the meadow", 0, 2),
144 passage("Retreat from the cave", 1, 0),
145 ],
146 };
147
148 let mut simulation = OwnedSimulation::<Adventure>::new(adventure);
149
150 let describe =
151 |simulation: &OwnedSimulation<Adventure>| simulation.rooms[*simulation.data()].clone();
152
153 println!("{}", describe(&simulation));
154 for choice in simulation.callables() {
155 println!(" → {}", simulation.passages[choice.0].text);
156 }
157
158 let Some(first) = simulation.callables().next() else {
159 panic!("crossroads has choices");
160 };
161 assert!(simulation.try_call(first));
162 assert_eq!(simulation.state.room, 1);
163 println!("\n{}", describe(&simulation));
164
165 let Some(back) = simulation.revertables().next() else {
166 panic!("cave can be left");
167 };
168 assert!(simulation.try_revert(back));
169 assert_eq!(simulation.state.room, 0);
170 println!("\nBack: {}", describe(&simulation));
171}Sourcefn try_revert(&mut self, event: Self::Event) -> bool
fn try_revert(&mut self, event: Self::Event) -> bool
Tries to revert the provided event and returns if it was successful.
Examples found in repository?
examples/text_adventure.rs (line 168)
134fn main() {
135 let adventure = Adventure {
136 rooms: vec![
137 "You stand at a crossroads.".into(),
138 "A dark cave yawns before you.".into(),
139 "Sunlight breaks over a quiet meadow.".into(),
140 ],
141 passages: vec![
142 passage("Enter the cave", 0, 1),
143 passage("Walk to the meadow", 0, 2),
144 passage("Retreat from the cave", 1, 0),
145 ],
146 };
147
148 let mut simulation = OwnedSimulation::<Adventure>::new(adventure);
149
150 let describe =
151 |simulation: &OwnedSimulation<Adventure>| simulation.rooms[*simulation.data()].clone();
152
153 println!("{}", describe(&simulation));
154 for choice in simulation.callables() {
155 println!(" → {}", simulation.passages[choice.0].text);
156 }
157
158 let Some(first) = simulation.callables().next() else {
159 panic!("crossroads has choices");
160 };
161 assert!(simulation.try_call(first));
162 assert_eq!(simulation.state.room, 1);
163 println!("\n{}", describe(&simulation));
164
165 let Some(back) = simulation.revertables().next() else {
166 panic!("cave can be left");
167 };
168 assert!(simulation.try_revert(back));
169 assert_eq!(simulation.state.room, 0);
170 println!("\nBack: {}", describe(&simulation));
171}Sourcefn prepare_call(&mut self) -> CallState<'_, Self, Call>
fn prepare_call(&mut self) -> CallState<'_, Self, Call>
Prepares a safe helper to list callable elements and choose one to call.
Sourcefn prepare_revert(&mut self) -> CallState<'_, Self, Revert>
fn prepare_revert(&mut self) -> CallState<'_, Self, Revert>
Prepares a safe helper to list revertable elements and choose one to revert.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".