system_rust/simulation/
sim_time.rs

1use std::cmp::Ordering;
2use std::fmt::{Display, Formatter};
3use std::ops;
4
5#[derive(Clone, Copy)]
6/// Relative time-units for the simulation
7pub enum SimDelay {
8    PS(u128),
9    NS(u128),
10    US(u128),
11    MS(u128),
12    S(u128),
13    Min(u128),
14    H(u128),
15    D(u128),
16    Max,
17}
18
19impl SimDelay {
20    pub(crate) fn sr_delay(&self) -> SRDelay {
21        match self {
22            SimDelay::PS(delay) => SRDelay::Time(*delay),
23            SimDelay::NS(delay) => SRDelay::Time(*delay * 1000),
24            SimDelay::US(delay) => SRDelay::Time(*delay * 1000000),
25            SimDelay::MS(delay) => SRDelay::Time(*delay * 1000000000),
26            SimDelay::S(delay) => SRDelay::Time(*delay * 1000000000000),
27            SimDelay::Min(delay) => SRDelay::Time(*delay * 60000000000000),
28            SimDelay::H(delay) => SRDelay::Time(*delay * 3600000000000000),
29            SimDelay::D(delay) => SRDelay::Time(*delay * 24 * 3600000000000000),
30            SimDelay::Max => SRDelay::Time(u128::MAX / 2),
31        }
32    }
33}
34
35#[derive(Clone, Copy, Debug)]
36pub(crate) enum SRDelay {
37    Time(u128),
38    Delta,
39}
40
41impl SRDelay {
42    pub(crate) fn zero() -> SRDelay {
43        SRDelay::Time(0)
44    }
45}
46
47#[derive(Copy, Clone, Eq, PartialEq, Debug)]
48pub(crate) struct SRTime {
49    pub time: u128,
50    pub delta: u64,
51}
52
53impl SRTime {
54    pub const ZERO: Self = SRTime { time: 0, delta: 0 };
55    pub const MAX: Self = SRTime {
56        time: u128::MAX / 2,
57        delta: 0,
58    };
59}
60
61impl ops::Add<SRDelay> for SRTime {
62    type Output = SRTime;
63
64    fn add(self, rhs: SRDelay) -> Self::Output {
65        match rhs {
66            SRDelay::Time(time_difference) => SRTime {
67                time: self.time + time_difference,
68                delta: 0,
69            },
70            SRDelay::Delta => SRTime {
71                time: self.time,
72                delta: self.delta + 1,
73            },
74        }
75    }
76}
77
78impl PartialOrd<Self> for SRTime {
79    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
80        if (self.time == other.time) && (self.delta == other.delta) {
81            Option::Some(Ordering::Equal)
82        } else if (self.time < other.time)
83            || ((self.time == other.time) && (self.delta < other.delta))
84        {
85            Option::Some(Ordering::Less)
86        } else {
87            Option::Some(Ordering::Greater)
88        }
89    }
90}
91
92impl Ord for SRTime {
93    fn cmp(&self, other: &Self) -> Ordering {
94        self.partial_cmp(other).unwrap()
95    }
96}
97
98impl Display for SRTime {
99    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
100        write!(f, "time: {}, delta: {}", self.time, self.delta)
101    }
102}