event_simulation/
owned.rs

1use std::ops::{Deref, DerefMut};
2
3use crate::{Editable, EditableSimulationInfo};
4
5use super::{Simulation, SimulationInfo};
6
7/// An owned simulation that holds the simulation info and the simulation state.
8pub struct OwnedSimulation<Info: SimulationInfo> {
9    info: Info,
10    /// The simulation state.
11    pub state: Info::State,
12}
13
14impl<Info: SimulationInfo> OwnedSimulation<Info> {
15    /// Creates a new `OwnedSimulation` using the specified `info`.
16    pub fn new<T: Into<Info>>(info: T) -> Self {
17        let info = info.into();
18        let state = info.default_state();
19        Self { info, state }
20    }
21
22    /// Loads a new `OwnedSimulation` from `data` using the specified `info`.
23    pub fn from_data(info: Info, data: Info::LoadData) -> Result<Self, Info::StateLoadingError> {
24        let state = info.load_state(data)?;
25        Ok(Self { info, state })
26    }
27
28    /// Release the info from the simulation again and destroys all states.
29    pub fn release(self) -> Info {
30        self.info
31    }
32}
33
34impl<Info: SimulationInfo + Clone> Clone for OwnedSimulation<Info> {
35    fn clone(&self) -> Self {
36        let info = self.info.clone();
37        let state = unsafe { info.clone_state(&self.state) };
38        Self { info, state }
39    }
40}
41
42impl<Info: SimulationInfo> Deref for OwnedSimulation<Info> {
43    type Target = Info;
44
45    fn deref(&self) -> &Info {
46        &self.info
47    }
48}
49
50impl<Info: SimulationInfo> Simulation for OwnedSimulation<Info> {
51    type StateLoadingError = Info::StateLoadingError;
52    type AccessData = Info::AccessData;
53    type LoadData = Info::LoadData;
54    type Event = Info::Event;
55    type EventContainer<'a> = Info::EventContainer<'a>
56    where
57        Self: 'a;
58
59    #[inline]
60    fn data(&self) -> &Info::AccessData {
61        unsafe { self.info.data(&self.state) }
62    }
63
64    #[inline]
65    fn reload(&mut self, data: Info::LoadData) -> Result<(), Info::StateLoadingError> {
66        self.state = self.info.load_state(data)?;
67        Ok(())
68    }
69
70    #[inline]
71    fn callables(&self) -> Info::EventContainer<'_> {
72        Info::callables(&self.state)
73    }
74
75    #[inline]
76    fn callable(&self, event: Info::Event) -> bool {
77        Info::callable(&self.state, event)
78    }
79
80    #[inline]
81    unsafe fn call(&mut self, event: Info::Event) {
82        self.info.call(&mut self.state, event)
83    }
84
85    #[inline]
86    fn revertables(&self) -> Info::EventContainer<'_> {
87        Info::revertables(&self.state)
88    }
89
90    #[inline]
91    fn revertable(&self, event: Info::Event) -> bool {
92        Info::revertable(&self.state, event)
93    }
94
95    #[inline]
96    unsafe fn revert(&mut self, event: Info::Event) {
97        self.info.revert(&mut self.state, event)
98    }
99}
100
101impl<Info: EditableSimulationInfo> Editable for OwnedSimulation<Info> {
102    type Edit<'a> = OwnedSimulationEdit<'a, Info>
103    where
104        Self: 'a;
105
106    fn edit(&mut self) -> OwnedSimulationEdit<'_, Info> {
107        let edit = unsafe { self.info.edit() };
108        OwnedSimulationEdit {
109            edit,
110            state: &mut self.state,
111        }
112    }
113}
114
115/// Helper type for safely editing the info of a owned simulation without invalidating the state.
116pub struct OwnedSimulationEdit<'a, Info: EditableSimulationInfo + 'a> {
117    edit: Info::Edit<'a>,
118    state: &'a mut Info::State,
119}
120
121impl<Info: EditableSimulationInfo> Drop for OwnedSimulationEdit<'_, Info> {
122    fn drop(&mut self) {
123        unsafe { self.edit.refresh_state(self.state) }
124    }
125}
126
127impl<'a, Info: EditableSimulationInfo> Deref for OwnedSimulationEdit<'a, Info> {
128    type Target = Info::Edit<'a>;
129    fn deref(&self) -> &Info::Edit<'a> {
130        &self.edit
131    }
132}
133
134impl<'a, Info: EditableSimulationInfo> DerefMut for OwnedSimulationEdit<'a, Info> {
135    fn deref_mut(&mut self) -> &mut Info::Edit<'a> {
136        &mut self.edit
137    }
138}