blob: cc846b3b0d0f745421efaebfd5c526a93ce3b6c6 (
plain) (
blame)
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
|
use std::thread;
use std::time::Duration as StdDuration;
use time::{self, Duration, Timespec};
pub struct Timer {
due: Timespec,
duration: Duration,
}
impl Timer {
pub fn new(duration_in_ms: u64) -> Timer {
let duration = Duration::milliseconds(duration_in_ms as i64);
Timer {
due: time::get_time() + duration,
duration: duration,
}
}
pub fn await(&mut self) {
let diff = self.due - time::get_time();
if diff > time::Duration::zero() {
let amount = diff.num_milliseconds() as u64;
thread::sleep(StdDuration::from_millis(amount));
}
self.due = self.due + self.duration;
}
pub fn check(&mut self) -> bool {
if time::get_time() >= self.due {
self.due = self.due + self.duration;
true
} else {
false
}
}
pub fn reset(&mut self) {
self.due = time::get_time() + self.duration;
}
}
|