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