event_simulation/
owned.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
use std::ops::{Deref, DerefMut};

use crate::{Editable, EditableSimulationInfo};

use super::{Simulation, SimulationInfo};

/// An owned simulation that holds the simulation info and the simulation state.
pub struct OwnedSimulation<Info: SimulationInfo> {
    info: Info,
    /// The simulation state.
    pub state: Info::State,
}

impl<Info: SimulationInfo> OwnedSimulation<Info> {
    /// Creates a new `OwnedSimulation` using the specified `info`.
    pub fn new<T: Into<Info>>(info: T) -> Self {
        let info = info.into();
        let state = info.default_state();
        Self { info, state }
    }

    /// Loads a new `OwnedSimulation` from `data` using the specified `info`.
    pub fn from_data(info: Info, data: Info::LoadData) -> Result<Self, Info::StateLoadingError> {
        let state = info.load_state(data)?;
        Ok(Self { info, state })
    }

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

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

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

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

impl<Info: SimulationInfo> Simulation for OwnedSimulation<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(&mut 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(&mut self.state, event)
    }
}

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

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

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

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

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

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