pub struct OwnedSimulation<Info: SimulationInfo> {
pub state: Info::State,
/* private fields */
}Expand description
An owned simulation that holds the simulation info and the simulation state.
Fields§
§state: Info::StateThe simulation state.
Implementations§
Source§impl<Info: SimulationInfo> OwnedSimulation<Info>
impl<Info: SimulationInfo> OwnedSimulation<Info>
Sourcepub fn new<T: Into<Info>>(info: T) -> Self
pub fn new<T: Into<Info>>(info: T) -> Self
Creates a new OwnedSimulation using the specified info.
Examples found in repository?
examples/text_adventure.rs (line 146)
132fn main() {
133 let adventure = Adventure {
134 rooms: vec![
135 "You stand at a crossroads.".into(),
136 "A dark cave yawns before you.".into(),
137 "Sunlight breaks over a quiet meadow.".into(),
138 ],
139 passages: vec![
140 passage("Enter the cave", 0, 1),
141 passage("Walk to the meadow", 0, 2),
142 passage("Retreat from the cave", 1, 0),
143 ],
144 };
145
146 let mut simulation = OwnedSimulation::<Adventure>::new(adventure);
147
148 let describe =
149 |simulation: &OwnedSimulation<Adventure>| simulation.rooms[*simulation.data()].clone();
150
151 println!("{}", describe(&simulation));
152 for choice in simulation.callables() {
153 println!(" → {}", simulation.passages[choice.0].text);
154 }
155
156 let Some(first) = simulation.callables().next() else {
157 panic!("crossroads has choices");
158 };
159 assert!(simulation.try_call(first));
160 assert_eq!(simulation.state.room, 1);
161 println!("\n{}", describe(&simulation));
162
163 let Some(back) = simulation.revertables().next() else {
164 panic!("cave can be left");
165 };
166 assert!(simulation.try_revert(back));
167 assert_eq!(simulation.state.room, 0);
168 println!("\nBack: {}", describe(&simulation));
169}Trait Implementations§
Source§impl<Info: SimulationInfo + Clone> Clone for OwnedSimulation<Info>
impl<Info: SimulationInfo + Clone> Clone for OwnedSimulation<Info>
Source§impl<Info: SimulationInfo> Deref for OwnedSimulation<Info>
impl<Info: SimulationInfo> Deref for OwnedSimulation<Info>
Source§impl<Info: EditableSimulationInfo> Editable for OwnedSimulation<Info>
impl<Info: EditableSimulationInfo> Editable for OwnedSimulation<Info>
Source§type Edit<'a> = OwnedSimulationEdit<'a, Info>
where
Self: 'a
type Edit<'a> = OwnedSimulationEdit<'a, Info> where Self: 'a
The type used for safe edits and refreshing the state.
Source§fn edit(&mut self) -> OwnedSimulationEdit<'_, Info>
fn edit(&mut self) -> OwnedSimulationEdit<'_, Info>
Creates a type which allows safe edits to the info without invalidating the states,
and automatically refreshes the states when the edit type goes out of scope.
Source§impl<Info: SimulationInfo> Simulation for OwnedSimulation<Info>
impl<Info: SimulationInfo> Simulation for OwnedSimulation<Info>
Source§type StateLoadingError = <Info as SimulationInfo>::StateLoadingError
type StateLoadingError = <Info as SimulationInfo>::StateLoadingError
The error type returned when loading the simulation state fails.
Source§type LoadData = <Info as SimulationInfo>::LoadData
type LoadData = <Info as SimulationInfo>::LoadData
The type of data used to load the simulation state.
Source§fn reload(
&mut self,
data: Info::LoadData,
) -> Result<(), Info::StateLoadingError>
fn reload( &mut self, data: Info::LoadData, ) -> Result<(), Info::StateLoadingError>
Reloads the simulation state from the provided data. Read more
Source§fn 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.
Source§fn 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.
Source§fn 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.
Source§fn 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.
Source§impl<Info: SimulationInfo> SimulationState for OwnedSimulation<Info>
impl<Info: SimulationInfo> SimulationState for OwnedSimulation<Info>
Source§type AccessData = <Info as SimulationInfo>::AccessData
type AccessData = <Info as SimulationInfo>::AccessData
The type used to access the current state data.
Source§type Event = <Info as SimulationInfo>::Event
type Event = <Info as SimulationInfo>::Event
The type of events that can be called or reverted in the simulation.
Source§type EventContainer<'a> = <Info as SimulationInfo>::EventContainer<'a>
where
Self: 'a
type EventContainer<'a> = <Info as SimulationInfo>::EventContainer<'a> where Self: 'a
The type of container used to access the available events.
Source§fn data(&self) -> &Info::AccessData
fn data(&self) -> &Info::AccessData
Returns a reference to the data which represents the current state.
Source§fn callables(&self) -> Info::EventContainer<'_>
fn callables(&self) -> Info::EventContainer<'_>
Returns the events that can be called in the current state.
Source§fn callable(&self, event: Info::Event) -> bool
fn callable(&self, event: Info::Event) -> bool
Checks if the provided event can be called in the current state.
Source§fn revertables(&self) -> Info::EventContainer<'_>
fn revertables(&self) -> Info::EventContainer<'_>
Returns the events that can be reverted in the current state.
Source§fn revertable(&self, event: Info::Event) -> bool
fn revertable(&self, event: Info::Event) -> bool
Checks if the provided event can be reverted in the current state.
Auto Trait Implementations§
impl<Info> Freeze for OwnedSimulation<Info>
impl<Info> RefUnwindSafe for OwnedSimulation<Info>
impl<Info> Send for OwnedSimulation<Info>
impl<Info> Sync for OwnedSimulation<Info>
impl<Info> Unpin for OwnedSimulation<Info>
impl<Info> UnsafeUnpin for OwnedSimulation<Info>
impl<Info> UnwindSafe for OwnedSimulation<Info>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more