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
use std::fmt::{Display, Error, Formatter};
#[derive(Copy, Clone, Debug, Default)]
pub struct Timer {
time: f64,
start: Option<f64>,
}
impl Timer {
pub fn new() -> Self {
Timer {
time: 0.0,
start: None,
}
}
pub fn reset(&mut self) {
self.time = 0.0
}
pub fn start(&mut self) {
self.time = 0.0;
self.start = Some(instant::now());
}
pub fn pause(&mut self) {
if let Some(start) = self.start {
self.time += instant::now() - start;
}
self.start = None;
}
pub fn resume(&mut self) {
self.start = Some(instant::now());
}
pub fn time(&self) -> f64 {
self.time
}
}
impl Display for Timer {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}s", self.time)
}
}