pub struct Reference(/* private fields */);Expand description
A UUIDv7 that names one request, command or action, so its log lines can be found again.
It starts with its creation time in Unix milliseconds, so references sort
by time, and in text as well, which makes a time window a range filter
over references (Reference::range). The time can be read back from a
reference; use a random (v4) ID where it must stay hidden, such as tokens.
use rustclamp_core::Reference;
let first = Reference::new();
let second = Reference::new();
assert!(first < second);
assert!(first.to_string() < second.to_string(), "text sorts the same way");
assert_eq!(first.to_string().parse::<Reference>(), Ok(first));Implementations§
Source§impl Reference
impl Reference
Sourcepub fn new() -> Self
pub fn new() -> Self
A new reference. References made by this process are strictly increasing, even within one millisecond or across a clock step back: the 12 bits after the time count up within a millisecond (RFC 9562, section 6.2, method 1), and the last 62 bits are random. Past 4096 references in one millisecond the time runs ahead of the clock until the clock catches up, so ordering holds at any rate.
§Panics
When the operating system’s random source fails (/dev/urandom on
Unix, BCryptGenRandom on Windows): a guessable ID is worse than none.
Sourcepub fn created_at(&self) -> SystemTime
pub fn created_at(&self) -> SystemTime
When the reference was made, to the millisecond.
Sourcepub fn range(from: SystemTime, to: SystemTime) -> (Self, Self)
pub fn range(from: SystemTime, to: SystemTime) -> (Self, Self)
The lowest and highest reference that can be made between from and
to (both included, to the millisecond): every reference made in that
window lies between the two, in bytes and in text.
use std::time::{Duration, SystemTime};
use rustclamp_core::Reference;
let now = SystemTime::now();
let reference = Reference::new();
let (low, high) = Reference::range(now, now + Duration::from_secs(1));
assert!(low <= reference && reference <= high);