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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#![deny(missing_docs, unsafe_code)]
use std::time::{Duration, Instant};
pub trait ToDuration {
fn to_duration(&self) -> Duration;
}
impl ToDuration for Duration {
fn to_duration(&self) -> Duration {
*self
}
}
impl ToDuration for f32 {
fn to_duration(&self) -> Duration {
if self < &0.0 {
panic!("Attempted to convert negative f32 number to Duration");
}
let whole = *self as u64;
let fract = (self.fract() * 1e9) as u32;
Duration::new(whole, fract)
}
}
impl ToDuration for f64 {
fn to_duration(&self) -> Duration {
if self < &0.0 {
panic!("Attempted to convert negative f64 number to Duration");
}
let whole = *self as u64;
let fract = (self.fract() * 1e9) as u32;
Duration::new(whole, fract)
}
}
impl ToDuration for u8 {
fn to_duration(&self) -> Duration {
Duration::new(u64::from(*self), 0)
}
}
impl ToDuration for u16 {
fn to_duration(&self) -> Duration {
Duration::new(u64::from(*self), 0)
}
}
impl ToDuration for u32 {
fn to_duration(&self) -> Duration {
Duration::new(u64::from(*self), 0)
}
}
impl ToDuration for u64 {
fn to_duration(&self) -> Duration {
Duration::new(*self, 0)
}
}
impl ToDuration for u128 {
fn to_duration(&self) -> Duration {
Duration::new(*self as u64, 0)
}
}
impl ToDuration for usize {
fn to_duration(&self) -> Duration {
Duration::new(*self as u64, 0)
}
}
pub trait FromDuration {
fn from_duration(duration: Duration) -> Self;
}
impl FromDuration for Duration {
fn from_duration(duration: Duration) -> Self {
duration
}
}
impl FromDuration for u64 {
fn from_duration(duration: Duration) -> Self {
duration.as_secs()
}
}
impl FromDuration for u128 {
fn from_duration(duration: Duration) -> Self {
u128::from(duration.as_secs())
}
}
impl FromDuration for usize {
fn from_duration(duration: Duration) -> Self {
duration.as_secs() as usize
}
}
impl FromDuration for f32 {
fn from_duration(duration: Duration) -> Self {
duration.as_secs() as f32 + duration.subsec_nanos() as f32 / 1e9
}
}
impl FromDuration for f64 {
fn from_duration(duration: Duration) -> Self {
duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) / 1e9
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timer {
start: Instant,
}
impl Timer {
pub fn start() -> Timer {
Timer {
start: Instant::now(),
}
}
pub fn reset(&mut self) {
self.start = Instant::now();
}
pub fn elapsed(self) -> f64 {
f64::from_duration(self.duration())
}
pub fn duration(self) -> Duration {
Instant::now().duration_since(self.start)
}
pub fn started_at(self) -> Instant {
self.start
}
}
impl Default for Timer {
fn default() -> Self {
Timer::start()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EggTimer {
timer: Timer,
duration: Duration,
}
impl EggTimer {
pub fn set<D: ToDuration>(time: D) -> EggTimer {
EggTimer {
timer: Timer::start(),
duration: time.to_duration(),
}
}
pub fn reset(&mut self) {
self.timer = Timer::start();
}
pub fn duration_left(&self) -> Option<Duration> {
self.duration.checked_sub(self.timer.duration())
}
pub fn seconds_left(&self) -> f64 {
f64::from_duration(self.duration) - self.timer.elapsed()
}
pub fn is_ready(self) -> bool {
self.duration_left().is_none()
}
pub fn max_duration(&self) -> Duration {
self.duration
}
pub fn max_seconds(&self) -> f64 {
f64::from_duration(self.max_duration())
}
pub fn elapsed(&self) -> f64 {
self.timer.elapsed()
}
pub fn duration(&self) -> Duration {
self.timer.duration()
}
pub fn started_at(&self) -> Instant {
self.timer.started_at()
}
pub fn ends_at(&self) -> Instant {
self.timer.started_at() + self.duration
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Stopwatch {
last_start: Instant,
prev_dur: Duration,
paused: bool,
}
impl Stopwatch {
pub fn start() -> Stopwatch {
Stopwatch {
last_start: Instant::now(),
prev_dur: 0u64.to_duration(),
paused: false,
}
}
pub fn start_paused() -> Stopwatch {
Stopwatch {
last_start: Instant::now(),
prev_dur: 0u64.to_duration(),
paused: true,
}
}
pub fn reset(&mut self) {
self.last_start = Instant::now();
self.prev_dur = 0u64.to_duration();
}
pub fn elapsed(&self) -> f64 {
f64::from_duration(self.duration())
}
pub fn duration(&self) -> Duration {
if self.paused {
self.prev_dur
} else {
self.prev_dur + Instant::now().duration_since(self.last_start)
}
}
pub fn pause(&mut self) {
if !self.paused {
self.prev_dur += Instant::now().duration_since(self.last_start);
}
}
pub fn resume(&mut self) {
if self.paused {
self.last_start = Instant::now();
}
}
pub fn toggle(&mut self) {
if self.paused {
self.pause();
} else {
self.resume();
}
}
pub fn started_at(&self) -> Instant {
self.last_start
}
}
impl Default for Stopwatch {
fn default() -> Self {
Stopwatch::start()
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct TimedList<T> {
list: Vec<(EggTimer, T)>,
}
impl<T> TimedList<T> {
pub fn new() -> TimedList<T> {
TimedList { list: Vec::new() }
}
pub fn insert<D: ToDuration>(&mut self, element: T, time: D) {
self.list.push((EggTimer::set(time), element));
}
pub fn clean(&mut self) {
self.list.retain(|(timer, _)| !timer.is_ready());
}
pub fn clear(&mut self) {
self.list.clear();
}
pub fn len(&self) -> usize {
self.iter().count()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
self.list.retain(|(_, elem)| f(elem));
}
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> {
self.list.iter().filter_map(
|(timer, elem)| {
if timer.is_ready() {
None
} else {
Some(elem)
}
},
)
}
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
self.clean();
self.list.iter_mut().filter_map(
|(timer, elem)| {
if timer.is_ready() {
None
} else {
Some(elem)
}
},
)
}
pub fn timer_iter(&self) -> impl DoubleEndedIterator<Item = (&T, EggTimer)> {
self.list.iter().filter_map(|(timer, elem)| {
if timer.is_ready() {
None
} else {
Some((elem, *timer))
}
})
}
pub fn timer_iter_mut(&mut self) -> impl DoubleEndedIterator<Item = (&mut T, EggTimer)> {
self.clean();
self.list.iter_mut().filter_map(|(timer, elem)| {
if timer.is_ready() {
None
} else {
Some((elem, *timer))
}
})
}
}
impl<T, D> std::iter::FromIterator<(T, D)> for TimedList<T>
where
D: ToDuration,
{
fn from_iter<I: IntoIterator<Item = (T, D)>>(iter: I) -> Self {
TimedList {
list: iter
.into_iter()
.map(|(x, d)| (EggTimer::set(d), x))
.collect(),
}
}
}
impl<T> IntoIterator for TimedList<T>
where
T: 'static,
{
type Item = T;
type IntoIter = Box<DoubleEndedIterator<Item = T>>;
fn into_iter(mut self) -> Self::IntoIter {
self.clean();
Box::new(self.list.into_iter().filter_map(
|(timer, elem)| {
if timer.is_ready() {
None
} else {
Some(elem)
}
},
))
}
}
pub fn measure<F>(f: F) -> f64
where
F: FnOnce(),
{
let timer = Timer::start();
f();
timer.elapsed()
}