1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Time source and [`Timestamp`] type.
/// A timestamp expressed as nanoseconds since the Unix epoch.
///
/// Stored as a `u64`, so the representable range extends well beyond the
/// 22nd century. Operations are saturating to avoid panics on overflow.
///
/// # Example
///
/// ```
/// use audit_trail::Timestamp;
///
/// let t = Timestamp::from_nanos(1_700_000_000_000_000_000);
/// assert_eq!(t.as_nanos(), 1_700_000_000_000_000_000);
/// ```
;
/// Pluggable time source for the audit chain.
///
/// Implementations are expected to be monotonic with respect to successive
/// calls. The chain enforces monotonicity at append time and returns
/// [`crate::Error::NonMonotonicClock`] if a regression is observed.
///
/// # Example
///
/// ```
/// use audit_trail::{Clock, Timestamp};
///
/// /// A fixed clock useful for testing.
/// struct FixedClock(Timestamp);
///
/// impl Clock for FixedClock {
/// fn now(&self) -> Timestamp { self.0 }
/// }
///
/// let clock = FixedClock(Timestamp::from_nanos(42));
/// assert_eq!(clock.now().as_nanos(), 42);
/// ```
/// Wall-clock time source backed by [`std::time::SystemTime`]. Requires
/// the `std` feature.
///
/// Most production deployments want this; it returns nanoseconds since
/// the Unix epoch using the host's system clock. The host clock is **not**
/// strictly monotonic — if the operator adjusts time backwards, the next
/// [`crate::Chain::append`] will return [`crate::Error::NonMonotonicClock`].
/// Deployments that need a strictly-monotonic source should wrap a
/// monotonic instant in a custom [`Clock`] instead.
///
/// On the unusual case that `SystemTime::now()` is before the Unix epoch,
/// this returns [`Timestamp::EPOCH`] (0). On the equally-unusual case
/// that the system clock exceeds `u64::MAX` nanoseconds past the epoch
/// (year ~2554 and later), the value saturates at `u64::MAX`.
///
/// # Example
///
/// ```
/// use audit_trail::{Clock, SystemClock};
///
/// let clock = SystemClock::new();
/// let t = clock.now();
/// assert!(t.as_nanos() > 0);
/// ```
;