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
use crate::TimeServiceTrait;
#[cfg(any(test, feature = "async"))]
use crate::{Sleep, SleepTrait};
#[cfg(any(test, feature = "async"))]
use std::pin::Pin;
use std::{
thread,
time::{Duration, Instant},
};
#[derive(Copy, Clone, Debug, Default)]
pub struct RealTimeService;
#[cfg(any(test, feature = "async"))]
pub type RealSleep = tokio::time::Sleep;
impl RealTimeService {
pub fn new() -> Self {
Self {}
}
}
impl TimeServiceTrait for RealTimeService {
fn now(&self) -> Instant {
Instant::now()
}
fn now_unix_time(&self) -> Duration {
diem_infallible::duration_since_epoch()
}
#[cfg(any(test, feature = "async"))]
fn sleep(&self, duration: Duration) -> Sleep {
tokio::time::sleep(duration).into()
}
fn sleep_blocking(&self, duration: Duration) {
thread::sleep(duration);
}
}
#[cfg(any(test, feature = "async"))]
impl SleepTrait for RealSleep {
fn is_elapsed(&self) -> bool {
RealSleep::is_elapsed(self)
}
fn reset(self: Pin<&mut Self>, duration: Duration) {
let deadline = self.deadline() + duration;
RealSleep::reset(self, deadline);
}
fn reset_until(self: Pin<&mut Self>, deadline: Instant) {
RealSleep::reset(self, tokio::time::Instant::from_std(deadline));
}
}