event_simulation/
multi.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::ops::{Deref, DerefMut};

use crate::{Editable, EditableSimulationInfo};

use super::{Simulation, SimulationInfo};

/// A simulation with support for multiple states.
pub struct MultiSimulation<Info: SimulationInfo> {
    info: Info,
    states: Vec<Info::State>,
}

impl<Info: SimulationInfo> MultiSimulation<Info> {
    /// Creates a new `MultiSimulation` from the provided `info` with no states.
    pub fn new<T: Into<Info>>(info: T) -> Self {
        Self {
            info: info.into(),
            states: Vec::new(),
        }
    }

    /// Adds a new simulation state to the simulation and returns its index.
    pub fn add_simulation(&mut self) -> usize {
        let index = self.states.len();
        self.states.push(Info::default_state(&self.info));
        index
    }

    /// Adds a new simulation state loaded from the provided `data` to the simulation and returns its index.
    pub fn add_simulation_from_data(
        &mut self,
        data: Info::LoadData,
    ) -> Result<usize, Info::StateLoadingError> {
        let index = self.states.len();
        self.states.push(Info::load_state(&self.info, data)?);
        Ok(index)
    }

    /// Retrieves an immutable borrow of the simulation at the specified index.
    pub fn get(&self, index: usize) -> Option<SimulationBorrow<'_, Info>> {
        let state = self.states.get(index)?;
        Some(SimulationBorrow {
            info: &self.info,
            state,
        })
    }

    /// Retrieves a mutable borrow of the simulation at the specified index.
    pub fn get_mut(&mut self, index: usize) -> Option<SimulationBorrowMut<'_, Info>> {
        let state = self.states.get_mut(index)?;
        Some(SimulationBorrowMut {
            info: &self.info,
            state,
        })
    }

    /// Get access to all simulation states.
    pub fn states(&self) -> &[Info::State] {
        &self.states
    }

    /// Get mutable access to all simulation states.
    pub fn states_mut(&mut self) -> &mut [Info::State] {
        &mut self.states
    }

    /// Returns an iterator over the simulation states.
    pub fn iter(&self) -> Iter<'_, Info> {
        self.into_iter()
    }

    /// Returns a mutable iterator over the simulation states.
    pub fn iter_mut(&mut self) -> IterMut<'_, Info> {
        self.into_iter()
    }

    /// Release the info from the simulation again and destroys all states.
    pub fn release(self) -> Info {
        self.info
    }
}

impl<Info: SimulationInfo + Clone> Clone for MultiSimulation<Info> {
    fn clone(&self) -> Self {
        let info = self.info.clone();
        let states = self
            .states
            .iter()
            .map(|state| unsafe { info.clone_state(state) })
            .collect();
        Self { info, states }
    }
}

impl<Info: SimulationInfo> Deref for MultiSimulation<Info> {
    type Target = Info;

    fn deref(&self) -> &Info {
        &self.info
    }
}

/// A generic struct representing a borrowed simulation with an immutable or mutable reference to the state.
pub struct GenericSimulationBorrow<'a, Info: SimulationInfo, StateRef> {
    info: &'a Info,
    /// The referenced simulation state.
    pub state: StateRef,
}

impl<Info: SimulationInfo, S> Deref for GenericSimulationBorrow<'_, Info, S> {
    type Target = Info;

    fn deref(&self) -> &Info {
        self.info
    }
}

/// Represents a single immutable simulation.
pub type SimulationBorrow<'a, Info> =
    GenericSimulationBorrow<'a, Info, &'a <Info as SimulationInfo>::State>;

/// Represents a single mutable simulation.
pub type SimulationBorrowMut<'a, Info> =
    GenericSimulationBorrow<'a, Info, &'a mut <Info as SimulationInfo>::State>;

/// An iterator over the simulation states.
pub struct Iter<'a, Info: SimulationInfo> {
    info: &'a Info,
    states: std::slice::Iter<'a, Info::State>,
}

/// A mutable iterator over the simulation states.
pub struct IterMut<'a, Info: SimulationInfo> {
    info: &'a Info,
    states: std::slice::IterMut<'a, Info::State>,
}

impl<'a, Info: SimulationInfo> IntoIterator for &'a MultiSimulation<Info> {
    type Item = SimulationBorrow<'a, Info>;
    type IntoIter = Iter<'a, Info>;

    fn into_iter(self) -> Self::IntoIter {
        Iter {
            info: &self.info,
            states: self.states.iter(),
        }
    }
}

impl<'a, Info: SimulationInfo> IntoIterator for &'a mut MultiSimulation<Info> {
    type Item = SimulationBorrowMut<'a, Info>;
    type IntoIter = IterMut<'a, Info>;

    fn into_iter(self) -> Self::IntoIter {
        IterMut {
            info: &self.info,
            states: self.states.iter_mut(),
        }
    }
}

impl<'a, Info: SimulationInfo> Iterator for Iter<'a, Info> {
    type Item = SimulationBorrow<'a, Info>;

    fn next(&mut self) -> Option<Self::Item> {
        let state = self.states.next()?;
        Some(SimulationBorrow {
            info: self.info,
            state,
        })
    }
}

impl<'a, Info: SimulationInfo> Iterator for IterMut<'a, Info> {
    type Item = SimulationBorrowMut<'a, Info>;

    fn next(&mut self) -> Option<Self::Item> {
        let state = self.states.next()?;
        Some(SimulationBorrowMut {
            info: self.info,
            state,
        })
    }
}

impl<Info: SimulationInfo> Simulation for SimulationBorrowMut<'_, Info> {
    type StateLoadingError = Info::StateLoadingError;
    type AccessData = Info::AccessData;
    type LoadData = Info::LoadData;
    type Event = Info::Event;
    type EventContainer<'a> = Info::EventContainer<'a>
    where
        Self: 'a;

    #[inline]
    fn data(&self) -> &Info::AccessData {
        unsafe { self.info.data(self.state) }
    }

    #[inline]
    fn reload(&mut self, data: Info::LoadData) -> Result<(), Info::StateLoadingError> {
        *self.state = self.info.load_state(data)?;
        Ok(())
    }

    #[inline]
    fn callables(&self) -> Info::EventContainer<'_> {
        Info::callables(self.state)
    }

    #[inline]
    fn callable(&self, event: Info::Event) -> bool {
        Info::callable(self.state, event)
    }

    #[inline]
    unsafe fn call(&mut self, event: Info::Event) {
        self.info.call(self.state, event)
    }

    #[inline]
    fn revertables(&self) -> Info::EventContainer<'_> {
        Info::revertables(self.state)
    }

    #[inline]
    fn revertable(&self, event: Info::Event) -> bool {
        Info::revertable(self.state, event)
    }

    #[inline]
    unsafe fn revert(&mut self, event: Info::Event) {
        self.info.revert(self.state, event)
    }
}

impl<Info: EditableSimulationInfo> Editable for MultiSimulation<Info> {
    type Edit<'a> = MultiSimulationEdit<'a, Info>
    where
        Self: 'a;

    fn edit(&mut self) -> MultiSimulationEdit<'_, Info> {
        let edit = unsafe { self.info.edit() };
        MultiSimulationEdit {
            edit,
            states: &mut self.states,
        }
    }
}

/// Helper type for safely editing the info of a multi simulation without invalidating the state.
pub struct MultiSimulationEdit<'a, Info: EditableSimulationInfo + 'a> {
    edit: Info::Edit<'a>,
    states: &'a mut [Info::State],
}

impl<Info: EditableSimulationInfo> Drop for MultiSimulationEdit<'_, Info> {
    fn drop(&mut self) {
        for state in self.states.iter_mut() {
            unsafe { self.edit.refresh_state(state) }
        }
    }
}

impl<'a, Info: EditableSimulationInfo> Deref for MultiSimulationEdit<'a, Info> {
    type Target = Info::Edit<'a>;
    fn deref(&self) -> &Info::Edit<'a> {
        &self.edit
    }
}

impl<'a, Info: EditableSimulationInfo> DerefMut for MultiSimulationEdit<'a, Info> {
    fn deref_mut(&mut self) -> &mut Info::Edit<'a> {
        &mut self.edit
    }
}