Skip to main content

event_simulation/
borrowed.rs

1use std::ops::Deref;
2
3use crate::{Simulation, SimulationInfo, SimulationState};
4
5/// A borrowed simulation that holds an immutable reference to the simulation info
6/// and owns the simulation state.
7pub struct BorrowedSimulation<'a, Info: SimulationInfo> {
8    info: &'a Info,
9    /// The simulation state.
10    pub state: Info::State,
11}
12
13impl<'a, Info: SimulationInfo> BorrowedSimulation<'a, Info> {
14    /// Creates a new `BorrowedSimulation` which belongs to the specified `info`.
15    pub fn new(info: &'a Info) -> Self {
16        let state = info.default_state();
17        Self { info, state }
18    }
19
20    /// Loads a new `BorrowedSimulation` from `data` which belongs to the specified `info`.
21    ///
22    /// # Errors
23    /// Returns an error if a valid state cannot be loaded from `data`.
24    pub fn from_data(
25        info: &'a Info,
26        data: Info::LoadData,
27    ) -> Result<Self, Info::StateLoadingError> {
28        let state = info.load_state(data)?;
29        Ok(Self { info, state })
30    }
31}
32
33impl<Info: SimulationInfo + Clone> Clone for BorrowedSimulation<'_, Info> {
34    fn clone(&self) -> Self {
35        let info = &self.info;
36        let state = unsafe { info.clone_state(&self.state) };
37        Self { info, state }
38    }
39}
40
41impl<Info: SimulationInfo> Deref for BorrowedSimulation<'_, Info> {
42    type Target = Info;
43
44    fn deref(&self) -> &Info {
45        self.info
46    }
47}
48
49impl<Info: SimulationInfo> SimulationState for BorrowedSimulation<'_, Info> {
50    type AccessData = Info::AccessData;
51    type Event = Info::Event;
52    type EventContainer<'a>
53        = Info::EventContainer<'a>
54    where
55        Self: 'a;
56
57    #[inline]
58    fn data(&self) -> &Info::AccessData {
59        unsafe { self.info.data(&self.state) }
60    }
61
62    #[inline]
63    fn callables(&self) -> Info::EventContainer<'_> {
64        Info::callables(&self.state)
65    }
66
67    #[inline]
68    fn callable(&self, event: Info::Event) -> bool {
69        Info::callable(&self.state, event)
70    }
71
72    #[inline]
73    fn revertables(&self) -> Info::EventContainer<'_> {
74        Info::revertables(&self.state)
75    }
76
77    #[inline]
78    fn revertable(&self, event: Info::Event) -> bool {
79        Info::revertable(&self.state, event)
80    }
81}
82
83impl<Info: SimulationInfo> Simulation for BorrowedSimulation<'_, Info> {
84    type StateLoadingError = Info::StateLoadingError;
85    type LoadData = Info::LoadData;
86
87    #[inline]
88    fn reload(&mut self, data: Info::LoadData) -> Result<(), Info::StateLoadingError> {
89        self.state = self.info.load_state(data)?;
90        Ok(())
91    }
92
93    #[inline]
94    unsafe fn call(&mut self, event: Info::Event) {
95        unsafe { self.info.call(&mut self.state, event) }
96    }
97
98    #[inline]
99    unsafe fn revert(&mut self, event: Info::Event) {
100        unsafe { self.info.revert(&mut self.state, event) }
101    }
102}