1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/// # gwid
///
/// [](https://crates.io/crates/gwid) [](https://docs.rs/gwid/latest/gwid/)
///
/// Implementing [RFC 9562](https://www.rfc-editor.org/info/rfc9562/) ccompliant UUIDs (also known as GUIDs). The name gwid is derived from the phonetic pronunciation of GUID.
///
/// # UUID Version 4
///
/// UUIDv4 is a 128-bit UUID generated using cryptographically secure random bits, providing a high degree of uniqueness without encoding a timestamp or providing chronological ordering.
///
/// ## `generate() -> String`
///
/// **Usage**
///
/// ```rust
/// use gwid;
/// let uuid = gwid::v4::generate();
/// ```
///
/// ## `generate_raw() -> u128`
///
/// Returns the raw `u128` UUID. Use [`encode`](#encodeuuid-u128---string) to format raw `u128` as UUID.
///
/// **Usage**
///
/// ```rust
/// use gwid;
/// let uuid = gwid::v4::generate_raw();
/// let encoded = gwid::encode(uuid);
/// ```
///
/// # UUID Version 7
///
/// UUIDv7 is a 128-bit, time-ordered UUID that embeds a Unix timestamp (milliseconds) and uses random bits for uniqueness. Its time-based structure makes UUIDs naturally sortable by creation time.
///
/// ## `generate() -> String`
///
/// ```rust
/// use gwid;
/// let uuidv7 = gwid::v7::generate().expect("error generating");
/// ```
///
/// Generates UUIDv7 values with sub-millisecond [timestamp](#monotonic-precision) precision. Although thread-safe, UUIDs generated across multiple threads are not guaranteed to be globally monotonic.
///
/// For example, two UUIDs generated within the same sub-millisecond bucket may be out of order:
///
/// ```rust
/// // First UUID (greater)
/// let first = "01a1d02e-eedb-7995-82da-278257b8340d";
/// // Second UUID (smaller)
/// let second = "01a1d02e-eedb-7995-808d-0ba60b4a6dd4";
/// ```
///
/// If you require strict sub-millisecond monotonicity, use [`generate_strict()`](#generate_strict---string).
///
/// [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562/) does not require UUIDv7 random bits to be cryptographically secure. **To use cryptographically secure randomness, enable the [`v7-crypto-rand`](#v7-crypto-rand) feature flag.**
///
/// ## `generate_strict() -> String`
///
/// ```rust
/// use gwid;
/// let uuidv7 = gwid::v7::generate_strict().expect("error generating strict");
/// ```
///
/// Guarantees **[strict](#monotonic-precision) thread-local monotonicity**. By default, monotonicity is maintained independently per thread. To guarantee **strict global monotonicity across threads**, enable the [`v7-global-monotonicity`](#v7-global-monotonicity) feature flag.
///
/// [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562/) does not require UUIDv7 random bits to be cryptographically secure. **To use cryptographically secure randomness, enable the [`v7-crypto-rand`](#v7-crypto-rand) feature flag.**
///
/// ## `generate_raw() -> u128`
///
/// Behaves exactly like [`generate()`](#generate---string-1), but returns the raw `u128` UUID. Use [`encode`](#encodeuuid-u128---string) to format raw `u128` as UUID.
///
/// ## `generate_strict_raw() -> u128`
///
/// Behaves exactly like [`generate_strict()`](#generate_strict---string), but returns the raw `u128` UUID. Use [`encode`](#encodeuuid-u128---string) to format raw `u128` as UUID.
///
/// ## Monotonic Precision
///
/// | Feature | `generate()` | `generate_strict()` |
/// | ---------------------- | ------------------------------------ | --------------------------------- |
/// | Default | Thread-local timestamp* monotonicity | Thread-local strict* monotonicity |
/// | v7-global-monotonicity | Thread-local timestamp* monotonicity | Global strict* monotonicity |
///
/// \*_timestamp_ - guarantees sub-millisecond timestamp precision, but **does not guarantee monotonicity within the same sub-millisecond timestamp bucket**
/// <br>\*_strict_ - guarantees monotonicity within same sub-millisecond timestamp bucket
///
/// # Helper Functions
///
/// ## `encode(uuid: u128) -> String`
///
/// Encode a raw `u128` as UUID `String`. Formats as hexadecimal and inserts hyphens at proper indices.
///
/// ```rust
/// use gwid;
/// let raw_uuid: u128 = 54839052801522565720884485497537422415;
/// let encoded = gwid::encode(raw_uuid);
/// ```
///
/// ## `decode(uuid: &str) -> Result<u128, GWIDError>`
///
/// Get the underlying raw `u128` value from a UUID.
///
/// ```rust
/// use gwid;
/// let uuid_str: &str = "a60d0392-a436-4e0f-b560-9f763b1d99a1";
/// let raw_uuid = gwid::decode(uuid_str).expect("raw uuid");
/// ```
///
/// ## `decode_unchecked(uuid: &str) -> u128`
///
/// **Panics if something goes wrong during parsing!**
///
/// Get the underlying raw `u128` value from a UUID.
///
/// ```rust
/// use gwid;
/// let uuid_str: &str = "a60d0392-a436-4e0f-b560-9f763b1d99a1";
/// let raw_uuid = gwid::decode_unchecked(uuid_str);
/// ```
///
/// # Cargo Features
///
/// | Feature | Default | Description |
/// | ------------------------ | -------- | --------------------------------------------------------------------------- |
/// | `v7-global-monotonicity` | Disabled | Uses shared synchronization to maintain UUIDv7 monotonicity across threads. |
/// | `v7-crypto-rand` | Disabled | Uses cryptographically secure random bits for `random_b` |
///
/// ## `v7-global-monotonicity`
///
/// Enable the feature in your `Cargo.toml`:
///
/// ```toml
/// [dependencies]
/// gwid = { version = "0.1", features = ["v7-global-monotonicity"] }
/// ```
///
/// When enabled (default), UUIDv7 generation uses a shared `Mutex` to synchronize generator state across threads, ensuring globally monotonic UUID generation within the process.
///
/// When disabled, each thread maintains its own independent generator state using thread-local storage. This eliminates mutex overhead and can improve single-threaded and concurrent generation performance, but **monotonicity is only guaranteed within each individual thread**, not across threads.
///
/// ## `v7-crypto-rand`
///
/// Enable the feature in your `Cargo.toml`:
///
/// ```toml
/// [dependencies]
/// gwid = { version = "0.1", features = ["v7-crypto-rand"] }
/// ```
///
/// When enabled, UUIDv7 generation will use cryptographically secure bits for randomness (specifically, the `random_b` field).
///
/// [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562/) does not require UUIDv7 random bits to be cryptographically secure, but we give you the option.
pub
/// Encodes a `u128` as UUID.
/// Formats as hexadecimal and inserts hyphens at proper indices.
/// Unchecked method! Panics if something goes wrong.
/// Decodes a UUID `String` into a `u128`.
/// Decodes a UUID `String` into a `u128`.
// Clears UUID Version bits (bits 48, 49, 50, and 51 when counting left->right and starting at 0)
// 11111111111111111111111111111111111111111111111100001111111111111111111111111111111111111111111111111111111111111111111111111111
pub const CLEAR_VERSION_MASK: u128 = 0xFFFFFFFFFFFF0FFFFFFFFFFFFFFFFFFF;
// Clears UUID Variant bits (bits 64 and 65 when counting left->right and starting at 0)
// 11111111111111111111111111111111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111111111
pub const CLEAR_VARIANT_MASK: u128 = 0xFFFFFFFFFFFFFFFF3FFFFFFFFFFFFFFF;
pub const VERSION_SHIFT: u128 = 76;
pub const VARIANT_SHIFT: u128 = 62;
// Helper function to print bits
pub