event_simulation/lib.rs
1#![deny(missing_docs)]
2
3//! This crate provides a library for event based simulation of application state, particularly useful
4//! for representing progress and choices in video games or similar applications.
5//!
6//! The crate separates the simulation into two main components, the simulation info and the simulation
7//! state. The simulation info is immutable and defines the structure and rules of the simulation, while
8//! the simulation state is mutable and represents the current state of the simulation.
9//!
10//! The crate provides various types and traits to facilitate the interaction with the simulation, such
11//! as `Simulation`, `SimulationInfo`, `BorrowedSimulation`, `MultiSimulation`, and `OwnedSimulation`.
12//!
13//! For more detailed documentation and usage examples, please refer to the [crate documentation](https://docs.rs/event-simulation)
14//! or visit the [repository](https://gitlab.com/porky11/event-simulation).
15
16use std::ops::Deref;
17
18/// The `SimulationState` trait provides a read-only interface for inspecting the current state of a simulation.
19pub trait SimulationState {
20 /// The type used to access the current state data.
21 type AccessData: ?Sized;
22
23 /// The type of events that can be called or reverted in the simulation.
24 type Event: Copy + Ord;
25
26 /// The type of container used to access the available events.
27 type EventContainer<'a>: Iterator<Item = Self::Event>
28 where
29 Self: 'a;
30
31 /// Returns a reference to the data which represents the current state.
32 fn data(&self) -> &Self::AccessData;
33
34 /// Returns the events that can be called in the current state.
35 fn callables(&self) -> Self::EventContainer<'_>;
36
37 /// Returns the events that can be reverted in the current state.
38 fn revertables(&self) -> Self::EventContainer<'_>;
39
40 /// Checks if the provided event can be called in the current state.
41 fn callable(&self, event: Self::Event) -> bool;
42
43 /// Checks if the provided event can be reverted in the current state.
44 fn revertable(&self, event: Self::Event) -> bool;
45}
46
47/// The `Simulation` trait extends `SimulationState` by providing mutable access to modify the simulation state via events.
48pub trait Simulation: SimulationState {
49 /// The type of data used to load the simulation state.
50 type LoadData;
51
52 /// The error type returned when loading the simulation state fails.
53 type StateLoadingError;
54
55 /// Reloads the simulation state from the provided data.
56 fn reload(&mut self, data: Self::LoadData) -> Result<(), Self::StateLoadingError>;
57
58 /// Calls the provided event.
59 ///
60 /// # Safety
61 ///
62 /// The caller must ensure that the event is valid and can be called in the current simulation state.
63 unsafe fn call(&mut self, event: Self::Event);
64
65 /// Reverts the provided event.
66 ///
67 /// # Safety
68 ///
69 /// The caller must ensure that the event is valid and can be reverted in the current simulation state.
70 unsafe fn revert(&mut self, event: Self::Event);
71
72 /// Tries to call the provided event and returns if it was successful.
73 fn try_call(&mut self, event: Self::Event) -> bool {
74 if !self.callable(event) {
75 return false;
76 }
77
78 unsafe { self.call(event) }
79
80 true
81 }
82
83 /// Tries to revert the provided event and returns if it was successful.
84 fn try_revert(&mut self, event: Self::Event) -> bool {
85 if !self.revertable(event) {
86 return false;
87 }
88
89 unsafe { self.revert(event) }
90
91 true
92 }
93
94 /// Prepares a safe helper to list callable elements and choose one to call.
95 fn prepare_call(&mut self) -> CallState<'_, Self, Call> {
96 let callables = self.callables().collect();
97 CallState {
98 simulation: self,
99 callables,
100 direction: Call,
101 }
102 }
103
104 /// Prepares a safe helper to list revertable elements and choose one to revert.
105 fn prepare_revert(&mut self) -> CallState<'_, Self, Revert> {
106 let callables = self.revertables().collect();
107 CallState {
108 simulation: self,
109 callables,
110 direction: Revert,
111 }
112 }
113}
114
115/// A trait representing a direction for calling or reverting events in a simulation.
116pub trait PlayDirection {
117 /// Calls the provided event based on the direction.
118 ///
119 /// # Safety
120 /// This method can assume the parameter to event to be callable.
121 unsafe fn call<S: Simulation>(&self, simulation: &mut S, event: S::Event);
122}
123
124/// A type representing the forward direction for calling events in a simulation.
125pub struct Call;
126impl PlayDirection for Call {
127 unsafe fn call<S: Simulation>(&self, simulation: &mut S, event: S::Event) {
128 unsafe { simulation.call(event) }
129 }
130}
131
132/// A type representing the backward direction for reverting events in a simulation.
133pub struct Revert;
134impl PlayDirection for Revert {
135 unsafe fn call<S: Simulation>(&self, simulation: &mut S, event: S::Event) {
136 unsafe { simulation.revert(event) }
137 }
138}
139
140/// A temporary object for selecting an event to call.
141pub struct CallState<'a, S: Simulation + ?Sized, D: PlayDirection> {
142 simulation: &'a mut S,
143 /// A slice of indices of the current callable events.
144 pub callables: Box<[S::Event]>,
145 direction: D,
146}
147
148impl<S: Simulation, D: PlayDirection> CallState<'_, S, D> {
149 /// Calls the event at the specified index.
150 pub fn call(self, index: usize) {
151 let event = self.callables[index];
152 unsafe { self.direction.call(self.simulation, event) }
153 }
154
155 /// Attempts to call the event at the specified index and returns if it was successful.
156 pub fn try_call(self, index: usize) -> bool {
157 let Some(&event) = self.callables.get(index) else {
158 return false;
159 };
160 unsafe { self.direction.call(self.simulation, event) }
161 true
162 }
163}
164
165/// The `SimulationInfo` trait provides an interface for interacting with a simulation.
166///
167/// # Safety
168/// This trait contains methods marked as `unsafe` that require careful usage.
169/// The following invariants must be upheld when calling these methods:
170///
171/// 1. The `state` parameter must be compatible with the current `SimulationInfo` instance.
172/// Implementations of this trait may assume that the provided `state` is compatible.
173/// 2. When calling `call` or `revert`, the `state` must be callable or revertable for the specified `event`.
174///
175/// Violating these invariants may lead to undefined behavior or incorrect simulation results.
176pub trait SimulationInfo {
177 /// The type of the simulation state.
178 type State;
179
180 /// The error type returned when loading the simulation state fails.
181 type StateLoadingError;
182
183 /// The type used to access the current state.
184 type AccessData: ?Sized;
185
186 /// The type of data used to load the simulation state.
187 type LoadData;
188
189 /// The type of events that can be called or reverted in the simulation.
190 type Event: Copy + Ord;
191
192 /// The type of container used to access the available events.
193 type EventContainer<'a>: Iterator<Item = Self::Event>
194 where
195 Self: 'a;
196
197 /// Creates a new default state compatible with this `SimulationInfo` instance.
198 fn default_state(&self) -> Self::State;
199
200 /// Loads a state from the provided data, returning a `Result` with the loaded state or an error.
201 fn load_state(&self, data: Self::LoadData) -> Result<Self::State, Self::StateLoadingError>;
202
203 /// Clones the provided `state`, assuming it is compatible with this `SimulationInfo` instance.
204 ///
205 /// # Safety
206 /// The caller must ensure that the provided `state` is compatible with this `SimulationInfo` instance.
207 unsafe fn clone_state(&self, state: &Self::State) -> Self::State;
208
209 /// Returns a reference to the data which repsesents the `state`.
210 ///
211 /// # Safety
212 /// The caller must ensure that the provided `state` is compatible with this `SimulationInfo` instance.
213 unsafe fn data<'a>(&self, state: &'a Self::State) -> &'a Self::AccessData;
214
215 /// Returns the events that can be called for the provided `state`.
216 fn callables(state: &Self::State) -> Self::EventContainer<'_>;
217
218 /// Returns the events that can be reverted for the provided `state`.
219 fn revertables(state: &Self::State) -> Self::EventContainer<'_>;
220
221 /// Checks if the provided `event` can be called for the given `state`.
222 fn callable(state: &Self::State, event: Self::Event) -> bool;
223
224 /// Checks if the provided `event` can be reverted for the given `state`.
225 fn revertable(state: &Self::State, event: Self::Event) -> bool;
226
227 /// Calls the provided `event` on the given mutable `state`.
228 ///
229 /// # Safety
230 /// The caller must ensure that the provided `state` is compatible with this `SimulationInfo` instance
231 /// and that the `state` is callable for the specified `event`.
232 unsafe fn call(&self, state: &mut Self::State, event: Self::Event);
233
234 /// Reverts the provided `event` on the given mutable `state`.
235 ///
236 /// # Safety
237 /// The caller must ensure that the provided `state` is compatible with this `SimulationInfo` instance
238 /// and that the `state` is revertable for the specified `event`.
239 unsafe fn revert(&self, state: &mut Self::State, event: Self::Event);
240}
241
242/// The `EditalbeSimulationInfo` trait provides an interface for editing the simulation while ensuring the state to stay valid.
243pub trait EditableSimulationInfo: SimulationInfo {
244 /// The type used for safe edits.
245 type Edit<'a>: Deref<Target = Self>
246 where
247 Self: 'a;
248
249 /// Creates a type which allows safe edits to the info without invalidating the states.
250 ///
251 /// # Safety
252 /// After editing the info using the edit type, `refresh_state` has to be called before continuing the simulation.
253 unsafe fn edit(&mut self) -> Self::Edit<'_>;
254
255 /// Refreshes the provided mutable `state`, assuming it is compatible with this `SimulationInfo` instance.
256 ///
257 /// # Safety
258 /// The caller must ensure that the provided `state` is compatible with this `SimulationInfo` instance.
259 unsafe fn refresh_state(&self, state: &mut Self::State);
260}
261
262/// A trait for types that can be safely edited without invalidating their associated states.
263pub trait Editable {
264 /// The type used for safe edits and refreshing the state.
265 type Edit<'a>
266 where
267 Self: 'a;
268
269 /// Creates a type which allows safe edits to the info without invalidating the states,
270 /// and automatically refreshes the states when the edit type goes out of scope.
271 fn edit(&mut self) -> Self::Edit<'_>;
272}
273
274mod borrowed;
275mod multi;
276mod owned;
277
278pub use borrowed::BorrowedSimulation;
279pub use multi::{
280 MultiSimulation, MultiSimulationEdit, SimulationBorrow, SimulationBorrowMut,
281 SimulationBorrowRef,
282};
283pub use owned::{OwnedSimulation, OwnedSimulationEdit};
284
285mod dynamic;
286
287pub use dynamic::DynamicSimulation;