gpu_async_reactor/host/
tokio.rs1use tokio::time::Instant;
9
10use crate::core::time::{Clock, MonoNs, Timer};
11
12#[derive(Clone, Copy, Debug)]
14pub struct TokioClock {
15 origin: Instant,
16}
17
18impl TokioClock {
19 #[must_use]
21 pub fn new() -> Self {
22 Self {
23 origin: Instant::now(),
24 }
25 }
26}
27
28impl Default for TokioClock {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl Clock for TokioClock {
35 fn monotonic_ns(&self) -> MonoNs {
36 MonoNs(u64::try_from(self.origin.elapsed().as_nanos()).unwrap_or(u64::MAX))
37 }
38}
39
40impl Timer for TokioClock {
41 async fn sleep_until(&self, at: MonoNs) {
42 tokio::time::sleep_until(self.origin + core::time::Duration::from_nanos(at.0)).await;
43 }
44}