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
use crate::{TimeSpan, TimingMethod};
use std::ops::{Add, AddAssign, Index, IndexMut, Sub, SubAssign};
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
pub struct Time {
pub real_time: Option<TimeSpan>,
pub game_time: Option<TimeSpan>,
}
impl Time {
#[inline]
pub fn new() -> Self {
Time::default()
}
#[inline]
pub fn zero() -> Self {
Time {
real_time: Some(TimeSpan::zero()),
game_time: Some(TimeSpan::zero()),
}
}
#[inline]
pub fn with_real_time(self, real_time: Option<TimeSpan>) -> Self {
Time { real_time, ..self }
}
#[inline]
pub fn with_game_time(self, game_time: Option<TimeSpan>) -> Self {
Time { game_time, ..self }
}
#[inline]
pub fn with_timing_method(
mut self,
timing_method: TimingMethod,
time: Option<TimeSpan>,
) -> Self {
self[timing_method] = time;
self
}
pub fn op<F>(a: Time, b: Time, mut f: F) -> Time
where
F: FnMut(TimeSpan, TimeSpan) -> TimeSpan,
{
Time {
real_time: catch! { f(a.real_time?, b.real_time?) },
game_time: catch! { f(a.game_time?, b.game_time?) },
}
}
}
pub struct RealTime(pub Option<TimeSpan>);
impl From<RealTime> for Time {
fn from(t: RealTime) -> Time {
Time::new().with_real_time(t.0)
}
}
pub struct GameTime(pub Option<TimeSpan>);
impl From<GameTime> for Time {
fn from(t: GameTime) -> Time {
Time::new().with_game_time(t.0)
}
}
impl Add for Time {
type Output = Time;
fn add(self, rhs: Time) -> Self {
Time::op(self, rhs, Add::add)
}
}
impl AddAssign for Time {
fn add_assign(&mut self, rhs: Time) {
*self = *self + rhs;
}
}
impl Sub for Time {
type Output = Time;
fn sub(self, rhs: Time) -> Self {
Time::op(self, rhs, Sub::sub)
}
}
impl SubAssign for Time {
fn sub_assign(&mut self, rhs: Time) {
*self = *self - rhs;
}
}
impl Index<TimingMethod> for Time {
type Output = Option<TimeSpan>;
fn index(&self, timing_method: TimingMethod) -> &Self::Output {
match timing_method {
TimingMethod::RealTime => &self.real_time,
TimingMethod::GameTime => &self.game_time,
}
}
}
impl IndexMut<TimingMethod> for Time {
fn index_mut(&mut self, timing_method: TimingMethod) -> &mut Self::Output {
match timing_method {
TimingMethod::RealTime => &mut self.real_time,
TimingMethod::GameTime => &mut self.game_time,
}
}
}