Skip to main content

capsule_library/
ambient.rs

1//! The system implementations of `capsule-core`'s injected [`Clock`] and [`Rng`].
2//!
3//! `capsule-core` reads no clock and draws no identifier itself; every UUIDv7 it mints comes from
4//! an [`IdSource`] its caller builds. This module is where a client's real wall clock and the OS
5//! random source enter: [`system_ids`] is the source every production call in this crate passes.
6//! See [`capsule_core`] for the stateless core's rule.
7
8use capsule_core::ambient::{Clock, IdSource, Rng};
9use jiff::Timestamp;
10
11/// The system wall clock.
12#[derive(Debug, Clone, Copy, Default)]
13pub struct SystemClock;
14
15impl Clock for SystemClock {
16    fn now(&self) -> Timestamp {
17        Timestamp::now()
18    }
19}
20
21/// The OS random source, through `capsule-core`'s CSPRNG.
22#[derive(Debug, Clone, Copy, Default)]
23pub struct OsRng;
24
25impl Rng for OsRng {
26    fn fill_bytes(&self, buf: &mut [u8]) {
27        capsule_core::crypto::rng::fill(buf);
28    }
29}
30
31/// An identifier source over the [`SystemClock`] and [`OsRng`].
32pub fn system_ids() -> IdSource<'static> {
33    IdSource::new(&SystemClock, &OsRng)
34}
35
36#[cfg(test)]
37mod tests;