aimdb_core/clock.rs
1//! Instance time: one monotonic clock per database instance (design 058 ยง3).
2//!
3//! A [`Stamp`] is nanoseconds since the instance started (`build()`). Stamps
4//! are scoped to one instance lifetime: comparing stamps across a restart, or
5//! across two instances, is meaningless.
6
7/// Nanoseconds since the instance started.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "defmt", derive(defmt::Format))]
10pub struct Stamp(u64);
11
12impl Stamp {
13 pub const fn from_nanos(n: u64) -> Self {
14 Self(n)
15 }
16
17 pub const fn as_nanos(self) -> u64 {
18 self.0
19 }
20
21 /// `self โ earlier`, saturating at zero.
22 pub fn since(self, earlier: Stamp) -> core::time::Duration {
23 core::time::Duration::from_nanos(self.0.saturating_sub(earlier.0))
24 }
25
26 /// Clamps a `u128` nanosecond count (e.g. `Duration::as_nanos`) to a stamp,
27 /// saturating at `u64::MAX`.
28 pub(crate) fn saturating_from_nanos(n: u128) -> Self {
29 Self(u64::try_from(n).unwrap_or(u64::MAX))
30 }
31}
32
33/// The instance's clock, created once by `build()` through
34/// [`RuntimeOps::new_clock`](crate::RuntimeOps::new_clock).
35///
36/// The one source of time reads: two time sources with distinct guarantees.
37pub trait InstanceClock: Send + Sync {
38 /// Monotonic, instance-relative: time since the clock was created.
39 /// Nondecreasing (consecutive readings may be equal); saturates at
40 /// `u64::MAX`.
41 fn now(&self) -> Stamp;
42
43 /// Absolute wall-clock time as `(seconds, nanoseconds)` since the Unix
44 /// epoch. Independent of [`now`](Self::now)'s origin: it may be adjusted
45 /// (clock corrections) and is `None` where the platform has no wall clock
46 /// (e.g. a bare MCU with no RTC anchor). Never derive it from a stamp.
47 fn unix_time(&self) -> Option<(u64, u32)>;
48}
49
50/// Converts an elapsed time to a stamp, saturating at `u64::MAX`.
51///
52/// For adapters: `Duration::as_nanos` is `u128`.
53pub fn stamp_from_duration(d: core::time::Duration) -> Stamp {
54 Stamp::saturating_from_nanos(d.as_nanos())
55}
56
57/// Converts elapsed milliseconds as `f64` (e.g. `performance.now()`
58/// differences) to a stamp, clamped to `0..=u64::MAX`; `NaN` maps to zero.
59pub fn stamp_from_millis_f64(ms: f64) -> Stamp {
60 let ns = ms * 1_000_000.0;
61 // `as` saturates on float โ int and maps NaN to 0.
62 Stamp(ns as u64)
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use core::time::Duration;
69
70 #[test]
71 fn since_saturates_at_zero() {
72 let a = Stamp::from_nanos(10);
73 let b = Stamp::from_nanos(25);
74 assert_eq!(b.since(a), Duration::from_nanos(15));
75 assert_eq!(a.since(b), Duration::ZERO);
76 }
77
78 #[test]
79 fn duration_conversion_saturates() {
80 assert_eq!(stamp_from_duration(Duration::from_nanos(7)).as_nanos(), 7);
81 assert_eq!(stamp_from_duration(Duration::MAX).as_nanos(), u64::MAX);
82 }
83
84 #[test]
85 fn millis_f64_conversion_clamps() {
86 assert_eq!(stamp_from_millis_f64(1.5).as_nanos(), 1_500_000);
87 assert_eq!(stamp_from_millis_f64(-3.0).as_nanos(), 0);
88 assert_eq!(stamp_from_millis_f64(f64::NAN).as_nanos(), 0);
89 assert_eq!(stamp_from_millis_f64(f64::INFINITY).as_nanos(), u64::MAX);
90 }
91}