uuid7
A Rust implementation of the proposed UUID Version 7
use uuid7;
let uuid = uuid7;
println!; // e.g. "01809424-3e59-7c05-9219-566f82fff672"
println!; // as 16-byte big-endian array
let uuid_string: String = uuid7.to_string;
See draft-peabody-dispatch-new-uuid-format-03.
Field and bit layout
This implementation produces identifiers with the following bit layout:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| unix_ts_ms |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| unix_ts_ms | ver | counter |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var| counter |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| rand |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Where:
- The 48-bit
unix_ts_msfield is dedicated to the Unix timestamp in milliseconds. - The 4-bit
verfield is set at0111. - The 42-bit
counterfield accommodates the sequence counter that ensures the monotonic order of IDs generated within the same millisecond. The counter is incremented by one for each new ID generated within the same timestamp and is randomly initialized whenever theunix_ts_mschanges. - The 2-bit
varfield is set at10. - The remaining 32
randbits are filled with a cryptographically strong random number.
In the rare circumstances where the 42-bit counter field reaches its maximum
value and can no more be incremented within the same timestamp, this library
increments the unix_ts_ms; therefore, the unix_ts_ms may have a larger value
than that of the real-time clock. This library goes on with such larger
unix_ts_ms values caused by counter overflows and system clock rollbacks as
long as the difference from the system clock is small enough. If the system
clock moves back by ten seconds or more, this library resets the generator state
and thus breaks the monotonic order of generated identifiers.
Crate features
Default features:
stdenables the primaryuuid7()function. Withoutstd, this crate provides limited functionality available underno_stdenvironments.
Optional features:
serdeenables the serialization and deserialization ofUuidobjects.uuid(together withstd) enables thenew_v7()function that returns the popular uuid crate'sUuidobjects.
Other functionality
This library also supports the generation of UUID version 4:
use uuid4;
let uuid = uuid4;
println!; // e.g. "2ca4b2ce-6c13-40d4-bccf-37d222820f6f"
gen7::Generator provides a flexible interface to customize the various aspects
of the UUIDv7 generation:
use ;
let mut g = new;
let unix_ts_ms = 0x0123_4567_8901u64;
let = g.generate_core;
if status == ClockRollback else
License
Licensed under the Apache License, Version 2.0.