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
use super::time::{Duration, Instant};
use fail::fail_point;
#[derive(Debug, Copy, Clone)]
pub struct DeadlineError;
impl std::error::Error for DeadlineError {
fn description(&self) -> &str {
"deadline has elapsed"
}
}
impl std::fmt::Display for DeadlineError {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "deadline has elapsed")
}
}
#[derive(Debug, Copy, Clone)]
pub struct Deadline {
deadline: Instant,
}
impl Deadline {
pub fn new(deadline: Instant) -> Self {
Self { deadline }
}
pub fn from_now(after_duration: Duration) -> Self {
let deadline = Instant::now_coarse() + after_duration;
Self { deadline }
}
pub fn check(&self) -> std::result::Result<(), DeadlineError> {
fail_point!("deadline_check_fail", |_| Err(DeadlineError));
let now = Instant::now_coarse();
if self.deadline <= now {
return Err(DeadlineError);
}
Ok(())
}
}