pub struct TraceId(/* private fields */);Expand description
128-bit unique trace identifier.
Uses UUIDv7-style layout for time-ordered generation: the high 48 bits encode a millisecond Unix timestamp, the remaining 80 bits are random.
use franken_kernel::TraceId;
let id = TraceId::from_parts(1_700_000_000_000, 0xABCD_EF01_2345_6789_AB);
let hex = id.to_string();
let parsed: TraceId = hex.parse().unwrap();
assert_eq!(id, parsed);Implementations§
Source§impl TraceId
impl TraceId
Sourcepub const fn from_parts(ts_ms: u64, random: u128) -> Self
pub const fn from_parts(ts_ms: u64, random: u128) -> Self
Create a TraceId from a millisecond timestamp and random bits.
The high 48 bits store ts_ms, the low 80 bits store random.
The random value is truncated to 80 bits.
br-asupersync-97gwup: ts_ms is saturated to the 48-bit
representable range (2^48 - 1, ≈ year 10889) before being
shifted into the high bits. Pre-fix the function did
(ts_ms as u128) << 80 which silently dropped the high bits
of any ts_ms >= 2^48 — corrupting the trace-id wire layout
(the truncated bits would land in random’s space, producing
IDs that no longer satisfy the documented “high 48 = ts_ms,
low 80 = random” invariant). Saturation keeps the const-fn
signature and never silently corrupts the layout. Callers
that want a strict, no-saturation surface can use
Self::try_from_parts which returns Err on truncation.
Sourcepub const fn try_from_parts(ts_ms: u64, random: u128) -> Result<Self, u64>
pub const fn try_from_parts(ts_ms: u64, random: u128) -> Result<Self, u64>
Strict variant of Self::from_parts (br-asupersync-97gwup).
Returns Err(ts_ms) when the millisecond timestamp does not
fit in the 48-bit field. Use this in places where silently
saturating to year-10889 would mask a unit-error bug (e.g.,
passing microseconds or nanoseconds by mistake).
§Errors
Returns Err(ts_ms) if ts_ms >= 2^48.
Sourcepub const fn timestamp_ms(self) -> u64
pub const fn timestamp_ms(self) -> u64
Extract the millisecond timestamp from the high 48 bits.
Sourcepub const fn from_bytes(bytes: [u8; 16]) -> Self
pub const fn from_bytes(bytes: [u8; 16]) -> Self
Construct from big-endian bytes.