cap_primitives/time/
monotonic_clock.rs

1use crate::time::{Duration, Instant};
2use ambient_authority::AmbientAuthority;
3use std::time;
4
5/// A reference to a monotonically nondecreasing clock.
6///
7/// This does not directly correspond to anything in `std`, however its methods
8/// correspond to [methods in `std::time::Instant`].
9///
10/// [methods in `std::time::Instant`]: https://doc.rust-lang.org/std/time/struct.Instant.html#impl
11pub struct MonotonicClock(());
12
13impl MonotonicClock {
14    /// Constructs a new instance of `Self`.
15    ///
16    /// # Ambient Authority
17    ///
18    /// This uses ambient authority to accesses clocks.
19    #[inline]
20    pub const fn new(ambient_authority: AmbientAuthority) -> Self {
21        let _ = ambient_authority;
22        Self(())
23    }
24
25    /// Returns an instant corresponding to "now".
26    ///
27    /// This corresponds to [`std::time::Instant::now`].
28    #[inline]
29    pub fn now(&self) -> Instant {
30        Instant::from_std(time::Instant::now())
31    }
32
33    /// Returns the amount of time elapsed since this instant was created.
34    ///
35    /// This corresponds to [`std::time::Instant::elapsed`].
36    #[inline]
37    pub fn elapsed(&self, instant: Instant) -> Duration {
38        instant.std.elapsed()
39    }
40}