relay_cogs/
time.rs

1pub use std::time::Duration;
2
3#[cfg(not(test))]
4pub use self::real::*;
5#[cfg(test)]
6pub use self::test::*;
7
8#[cfg(not(test))]
9mod real {
10    pub use std::time::Instant;
11}
12
13#[cfg(test)]
14mod test {
15    use std::sync::atomic::{AtomicU64, Ordering};
16    use std::time::Duration;
17
18    std::thread_local! {
19        static NOW: AtomicU64 = const { AtomicU64::new(0) };
20    }
21
22    fn now() -> u64 {
23        NOW.with(|now| now.load(Ordering::Relaxed))
24    }
25
26    pub fn advance_millis(time: u64) {
27        NOW.with(|now| now.fetch_add(time, Ordering::Relaxed));
28    }
29
30    pub struct Instant(u64);
31
32    impl Instant {
33        pub fn now() -> Self {
34            Self(now())
35        }
36
37        pub fn elapsed(&self) -> Duration {
38            match now() - self.0 {
39                0 => Duration::from_nanos(100),
40                v => Duration::from_millis(v),
41            }
42        }
43    }
44}