A Rust implementation of the proposed UUID Version 7
```rust use uuid7::uuid7;
let uuid = uuid7(); println!("{}", uuid); // e.g. "01809424-3e59-7c05-9219-566f82fff672" println!("{:?}", uuid.as_bytes()); // as 16-byte big-endian array
let uuidstring: String = uuid7().tostring(); ```
See draft-peabody-dispatch-new-uuid-format-04.
This implementation produces identifiers with the following bit layout:
text
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:
unix_ts_ms
field is dedicated to the Unix timestamp in
milliseconds.ver
field is set at 0111
.counter
field 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 the unix_ts_ms
changes.var
field is set at 10
.rand
bits 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.
Default features:
std
enables the primary uuid7()
function. Without std
, this crate
provides limited functionality available under no_std
environments.Optional features:
serde
enables the serialization and deserialization of Uuid
objects.uuid
(together with std
) enables the new_v7()
function that returns the
popular uuid crate's Uuid
objects.This library also supports the generation of UUID version 4:
```rust use uuid7::uuid4;
let uuid = uuid4(); println!("{}", uuid); // e.g. "2ca4b2ce-6c13-40d4-bccf-37d222820f6f" ```
gen7::Generator
provides a flexible interface to customize the various aspects
of the UUIDv7 generation:
```rust use uuid7::gen7::{Generator, Status};
let mut g = Generator::new(rand::rngs::OsRng); let unixtsms = 0x012345678901u64; let (uuid, status) = g.generatecore(unixts_ms); if status == Status::ClockRollback { panic!("clock moved back; monotonic order was broken"); } else { println!("{}", uuid); } ```
Licensed under the Apache License, Version 2.0.