event_simulation/
dynamic.rs

1use std::ops::Deref;
2
3use crate::{Editable, SimulationInfo};
4
5/// A dynamic type, able to store both net kinds.
6pub enum DynamicSimulation<S: SimulationInfo, E: Editable> {
7    /// Variant for the default `S` type.
8    Default(S),
9    /// Variant for the `E` type.
10    Simulated(E),
11}
12
13impl<S: SimulationInfo, E: Editable> DynamicSimulation<S, E> {
14    /// Convert the dynamic net into a `S`
15    pub fn default(&self) -> Option<&S> {
16        if let Self::Default(info) = self {
17            Some(info)
18        } else {
19            None
20        }
21    }
22    /// Convert the dynamic net into a mutable `S`
23    pub fn default_mut(&mut self) -> Option<&mut S> {
24        if let Self::Default(info) = self {
25            Some(info)
26        } else {
27            None
28        }
29    }
30    /// Convert the dynamic net into a `E`
31    pub fn simulated(&self) -> Option<&E> {
32        if let Self::Simulated(sim) = self {
33            Some(sim)
34        } else {
35            None
36        }
37    }
38    /// Convert the dynamic net into a mutable `E`
39    pub fn simulated_mut(&mut self) -> Option<&mut E> {
40        if let Self::Simulated(sim) = self {
41            Some(sim)
42        } else {
43            None
44        }
45    }
46}
47
48impl<S: SimulationInfo, E: Editable + Deref<Target = S>> Deref for DynamicSimulation<S, E> {
49    type Target = S;
50    fn deref(&self) -> &S {
51        match self {
52            Self::Default(info) => info,
53            Self::Simulated(sim) => sim,
54        }
55    }
56}
57
58impl<S: SimulationInfo, E: Editable> From<S> for DynamicSimulation<S, E> {
59    fn from(info: S) -> Self {
60        Self::Default(info)
61    }
62}