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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{iter, thread, time};
pub struct Clock {
started_at: time::Instant,
tick_len: time::Duration,
}
pub struct ClockIter<'a>(&'a Clock);
impl Clock {
#[inline]
pub fn new(tick_len: time::Duration) -> Clock {
Clock::new_with_start_time(tick_len, time::Instant::now())
}
#[inline]
pub fn new_with_start_time(tick_len: time::Duration, start: time::Instant) -> Clock {
Clock {
started_at: start,
tick_len,
}
}
#[inline]
pub fn framerate(fps: f64) -> Clock {
Clock::framerate_with_start_time(fps, time::Instant::now())
}
#[inline]
pub fn framerate_with_start_time(fps: f64, start: time::Instant) -> Clock {
let frame_time_s = 1.0 / fps;
Clock::new_with_start_time(time::Duration::from_secs_f64(frame_time_s), start)
}
#[inline]
pub fn synced(&self, tick_len: time::Duration) -> Clock {
Clock {
started_at: self.started_at,
tick_len,
}
}
#[inline]
pub fn started_at(&self) -> time::Instant {
self.started_at
}
#[inline]
pub fn tick_num_at(&self, now: time::Instant) -> u128 {
(now - self.started_at).as_nanos() / self.tick_len.as_nanos()
}
#[inline]
pub fn wait_until_tick(&self) -> (u128, time::Instant) {
let now = time::Instant::now();
let current_tick_num = self.tick_num_at(now);
let next_tick_num = current_tick_num + 1;
let next_tick = self.started_at + self.tick_len * next_tick_num as u32;
let until_next: time::Duration = next_tick - now;
thread::sleep(until_next);
(next_tick_num, next_tick)
}
#[inline]
pub fn iter(&self) -> ClockIter {
ClockIter(self)
}
#[inline]
pub fn rel_iter(&self) -> ClockIterRelative {
ClockIterRelative(self)
}
}
impl<'a> iter::Iterator for ClockIter<'a> {
type Item = (u128, time::Instant);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some(self.0.wait_until_tick())
}
}
pub struct ClockIterRelative<'a>(&'a Clock);
impl<'a> iter::Iterator for ClockIterRelative<'a> {
type Item = (u128, time::Duration);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let (n, t) = self.0.wait_until_tick();
Some((n, t - self.0.started_at))
}
}