ClockSequence::usable_bits default returns bytes instead of bits, truncating custom v7 counters #866
Labels
No labels
breaking
bug
dependencies
good first issue
help wanted
unstable
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-uuid-rs/uuid#866
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The default
ClockSequence::usable_bits()implementation currently returnscore::mem::size_of::<Self::Output>(), which is a byte count, but the method is documented and consumed as a bit count.For custom
ClockSequenceimplementations that rely on the default,Timestamp::{now,from_unix}stores that value inusable_counter_bits, andUuid::new_v7then uses it as the number of counter bits to preserve in the UUID.That means the default path silently truncates custom counters:
u16defaults to2instead of16u32defaults to4instead of32u64defaults to8instead of64u128defaults to16instead of128Reproduction
Impact
This appears to affect third-party/custom
ClockSequenceimplementations that rely on the defaultusable_bits().The built-in contexts override
usable_bits(), soContext,ContextV7, andNoContextdo not appear to be affected.For affected custom v7 generators, the effective counter space is much smaller than the API contract suggests:
u16: 16 bits collapse to 2 bits (4states)u32: 32 bits collapse to 4 bits (16states)u64: 64 bits collapse to 8 bits (256states)u128: 128 bits collapse to 16 bits (65,536states)This can silently reduce monotonic sequencing/uniqueness entropy for high-rate custom v7 UUID generation.
Suggested fix
Change the default implementation to count bits instead of bytes:
Thanks @meng-xu-cs! This is a straight up bug