Skip to main content

engine/core/
timestamp.rs

1//! Virtual timestamps.
2//!
3//! Timestamps are produced by the Engine's [`Clock`](super::time::Clock). They
4//! count nanoseconds since an arbitrary virtual epoch and have no relation to
5//! the host wall clock unless the host clock implementation is used.
6
7/// A point in virtual time, in nanoseconds since the virtual epoch.
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
9pub struct Timestamp(u64);
10
11impl Timestamp {
12    /// The zero timestamp (the virtual epoch itself).
13    pub const ZERO: Self = Self(0);
14
15    /// Creates a timestamp from a raw nanosecond count.
16    pub const fn from_nanos(nanos: u64) -> Self {
17        Self(nanos)
18    }
19
20    /// Returns the raw nanosecond count.
21    pub const fn as_nanos(self) -> u64 {
22        self.0
23    }
24}
25
26impl std::fmt::Display for Timestamp {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{}ns", self.0)
29    }
30}