/rust/registry/src/index.crates.io-1949cf8c6b5b557f/uuid-1.23.0/src/lib.rs
Line | Count | Source |
1 | | // Copyright 2013-2014 The Rust Project Developers. |
2 | | // Copyright 2018 The Uuid Project Developers. |
3 | | // |
4 | | // See the COPYRIGHT file at the top-level directory of this distribution. |
5 | | // |
6 | | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
7 | | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
8 | | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
9 | | // option. This file may not be copied, modified, or distributed |
10 | | // except according to those terms. |
11 | | |
12 | | //! Generate and parse universally unique identifiers (UUIDs). |
13 | | //! |
14 | | //! Here's an example of a UUID: |
15 | | //! |
16 | | //! ```text |
17 | | //! 67e55044-10b1-426f-9247-bb680e5fe0c8 |
18 | | //! ``` |
19 | | //! |
20 | | //! A UUID is a unique 128-bit value, stored as 16 octets, and regularly |
21 | | //! formatted as a hex string in five groups. UUIDs are used to assign unique |
22 | | //! identifiers to entities without requiring a central allocating authority. |
23 | | //! |
24 | | //! They are particularly useful in distributed systems, though can be used in |
25 | | //! disparate areas, such as databases and network protocols. Typically a UUID |
26 | | //! is displayed in a readable string form as a sequence of hexadecimal digits, |
27 | | //! separated into groups by hyphens. |
28 | | //! |
29 | | //! The uniqueness property is not strictly guaranteed, however for all |
30 | | //! practical purposes, it can be assumed that an unintentional collision would |
31 | | //! be extremely unlikely. |
32 | | //! |
33 | | //! UUIDs have a number of standardized encodings that are specified in [RFC 9562](https://www.ietf.org/rfc/rfc9562.html). |
34 | | //! |
35 | | //! # Getting started |
36 | | //! |
37 | | //! Add the following to your `Cargo.toml`: |
38 | | //! |
39 | | //! ```toml |
40 | | //! [dependencies.uuid] |
41 | | //! version = "1.23.0" |
42 | | //! # Lets you generate random UUIDs |
43 | | //! features = [ |
44 | | //! "v4", |
45 | | //! ] |
46 | | //! ``` |
47 | | //! |
48 | | //! When you want a UUID, you can generate one: |
49 | | //! |
50 | | //! ``` |
51 | | //! # fn main() { |
52 | | //! # #[cfg(feature = "v4")] |
53 | | //! # { |
54 | | //! use uuid::Uuid; |
55 | | //! |
56 | | //! let id = Uuid::new_v4(); |
57 | | //! # } |
58 | | //! # } |
59 | | //! ``` |
60 | | //! |
61 | | //! If you have a UUID value, you can use its string literal form inline: |
62 | | //! |
63 | | //! ``` |
64 | | //! use uuid::{uuid, Uuid}; |
65 | | //! |
66 | | //! const ID: Uuid = uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8"); |
67 | | //! ``` |
68 | | //! |
69 | | //! # Working with different UUID versions |
70 | | //! |
71 | | //! This library supports all standardized methods for generating UUIDs through individual Cargo features. |
72 | | //! |
73 | | //! By default, this crate depends on nothing but the Rust standard library and can parse and format |
74 | | //! UUIDs, but cannot generate them. Depending on the kind of UUID you'd like to work with, there |
75 | | //! are Cargo features that enable generating them: |
76 | | //! |
77 | | //! * `v1` - Version 1 UUIDs using a timestamp and monotonic counter. |
78 | | //! * `v3` - Version 3 UUIDs based on the MD5 hash of some data. |
79 | | //! * `v4` - Version 4 UUIDs with random data. |
80 | | //! * `v5` - Version 5 UUIDs based on the SHA1 hash of some data. |
81 | | //! * `v6` - Version 6 UUIDs using a timestamp and monotonic counter. |
82 | | //! * `v7` - Version 7 UUIDs using a Unix timestamp. |
83 | | //! * `v8` - Version 8 UUIDs using user-defined data. |
84 | | //! |
85 | | //! This library also includes a [`Builder`] type that can be used to help construct UUIDs of any |
86 | | //! version without any additional dependencies or features. It's a lower-level API than [`Uuid`] |
87 | | //! that can be used when you need control over implicit requirements on things like a source |
88 | | //! of randomness. |
89 | | //! |
90 | | //! ## Which UUID version should I use? |
91 | | //! |
92 | | //! If you just want to generate unique identifiers then consider version 4 (`v4`) UUIDs. If you want |
93 | | //! to use UUIDs as database keys or need to sort them then consider version 7 (`v7`) UUIDs. |
94 | | //! Other versions should generally be avoided unless there's an existing need for them. |
95 | | //! |
96 | | //! Some UUID versions supersede others. Prefer version 6 over version 1 and version 5 over version 3. |
97 | | //! |
98 | | //! # Other features |
99 | | //! |
100 | | //! Other crate features can also be useful beyond the version support: |
101 | | //! |
102 | | //! * `serde` - adds the ability to serialize and deserialize a UUID using |
103 | | //! `serde`. |
104 | | //! * `borsh` - adds the ability to serialize and deserialize a UUID using |
105 | | //! `borsh`. |
106 | | //! * `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid` for |
107 | | //! fuzzing. |
108 | | //! * `fast-rng` - uses a faster algorithm for generating random UUIDs when available. |
109 | | //! This feature requires more dependencies to compile, but is just as suitable for |
110 | | //! UUIDs as the default algorithm. |
111 | | //! * `rng-rand` - forces `rand` as the backend for randomness. |
112 | | //! * `rng-getrandom` - forces `getrandom` as the backend for randomness. |
113 | | //! * `bytemuck` - adds a `Pod` trait implementation to `Uuid` for byte manipulation |
114 | | //! |
115 | | //! # Unstable features |
116 | | //! |
117 | | //! Some features are unstable. They may be incomplete or depend on other |
118 | | //! unstable libraries. These include: |
119 | | //! |
120 | | //! * `zerocopy` - adds support for zero-copy deserialization using the |
121 | | //! `zerocopy` library. |
122 | | //! |
123 | | //! Unstable features may break between minor releases. |
124 | | //! |
125 | | //! To allow unstable features, you'll need to enable the Cargo feature as |
126 | | //! normal, but also pass an additional flag through your environment to opt-in |
127 | | //! to unstable `uuid` features: |
128 | | //! |
129 | | //! ```text |
130 | | //! RUSTFLAGS="--cfg uuid_unstable" |
131 | | //! ``` |
132 | | //! |
133 | | //! # Building for other targets |
134 | | //! |
135 | | //! ## WebAssembly |
136 | | //! |
137 | | //! For WebAssembly, enable the `js` feature: |
138 | | //! |
139 | | //! ```toml |
140 | | //! [dependencies.uuid] |
141 | | //! version = "1.23.0" |
142 | | //! features = [ |
143 | | //! "v4", |
144 | | //! "v7", |
145 | | //! "js", |
146 | | //! ] |
147 | | //! ``` |
148 | | //! |
149 | | //! ## Embedded |
150 | | //! |
151 | | //! For embedded targets without the standard library, you'll need to |
152 | | //! disable default features when building `uuid`: |
153 | | //! |
154 | | //! ```toml |
155 | | //! [dependencies.uuid] |
156 | | //! version = "1.23.0" |
157 | | //! default-features = false |
158 | | //! ``` |
159 | | //! |
160 | | //! Some additional features are supported in no-std environments: |
161 | | //! |
162 | | //! * `v1`, `v3`, `v5`, `v6`, and `v8`. |
163 | | //! * `serde`. |
164 | | //! |
165 | | //! If you need to use `v4` or `v7` in a no-std environment, you'll need to |
166 | | //! produce random bytes yourself and then pass them to [`Builder::from_random_bytes`] |
167 | | //! without enabling the `v4` or `v7` features. |
168 | | //! |
169 | | //! If you're using `getrandom`, you can specify the `rng-getrandom` or `rng-rand` |
170 | | //! features of `uuid` and configure `getrandom`'s provider per its docs. `uuid` |
171 | | //! may upgrade its version of `getrandom` in minor releases. |
172 | | //! |
173 | | //! # Examples |
174 | | //! |
175 | | //! Parse a UUID given in the simple format and print it as a URN: |
176 | | //! |
177 | | //! ``` |
178 | | //! # use uuid::Uuid; |
179 | | //! # fn main() -> Result<(), uuid::Error> { |
180 | | //! let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?; |
181 | | //! |
182 | | //! println!("{}", my_uuid.urn()); |
183 | | //! # Ok(()) |
184 | | //! # } |
185 | | //! ``` |
186 | | //! |
187 | | //! Generate a random UUID and print it out in hexadecimal form: |
188 | | //! |
189 | | //! ``` |
190 | | //! // Note that this requires the `v4` feature to be enabled. |
191 | | //! # use uuid::Uuid; |
192 | | //! # fn main() { |
193 | | //! # #[cfg(feature = "v4")] { |
194 | | //! let my_uuid = Uuid::new_v4(); |
195 | | //! |
196 | | //! println!("{}", my_uuid); |
197 | | //! # } |
198 | | //! # } |
199 | | //! ``` |
200 | | //! |
201 | | //! # References |
202 | | //! |
203 | | //! * [Wikipedia: Universally Unique Identifier](http://en.wikipedia.org/wiki/Universally_unique_identifier) |
204 | | //! * [RFC 9562: Universally Unique IDentifiers (UUID)](https://www.ietf.org/rfc/rfc9562.html). |
205 | | //! |
206 | | //! [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen |
207 | | |
208 | | #![no_std] |
209 | | #![deny(missing_debug_implementations, missing_docs)] |
210 | | #![allow(clippy::mixed_attributes_style)] |
211 | | #![doc( |
212 | | html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", |
213 | | html_favicon_url = "https://www.rust-lang.org/favicon.ico", |
214 | | html_root_url = "https://docs.rs/uuid/1.23.0" |
215 | | )] |
216 | | |
217 | | #[cfg(any(feature = "std", test))] |
218 | | #[macro_use] |
219 | | extern crate std; |
220 | | |
221 | | #[cfg(all(not(feature = "std"), not(test)))] |
222 | | #[macro_use] |
223 | | extern crate core as std; |
224 | | |
225 | | #[macro_use] |
226 | | mod macros; |
227 | | |
228 | | mod builder; |
229 | | mod error; |
230 | | mod non_nil; |
231 | | mod parser; |
232 | | |
233 | | pub mod fmt; |
234 | | pub mod timestamp; |
235 | | |
236 | | use core::hash::{Hash, Hasher}; |
237 | | pub use timestamp::{context::NoContext, ClockSequence, Timestamp}; |
238 | | |
239 | | #[cfg(any(feature = "v1", feature = "v6"))] |
240 | | #[allow(deprecated)] |
241 | | pub use timestamp::context::Context; |
242 | | |
243 | | #[cfg(any(feature = "v1", feature = "v6"))] |
244 | | pub use timestamp::context::ContextV1; |
245 | | |
246 | | #[cfg(feature = "v7")] |
247 | | pub use timestamp::context::ContextV7; |
248 | | |
249 | | #[cfg(feature = "v1")] |
250 | | #[doc(hidden)] |
251 | | // Soft-deprecated (Rust doesn't support deprecating re-exports) |
252 | | // Use `Context` from the crate root instead |
253 | | pub mod v1; |
254 | | #[cfg(feature = "v3")] |
255 | | mod v3; |
256 | | #[cfg(feature = "v4")] |
257 | | mod v4; |
258 | | #[cfg(feature = "v5")] |
259 | | mod v5; |
260 | | #[cfg(feature = "v6")] |
261 | | mod v6; |
262 | | #[cfg(feature = "v7")] |
263 | | mod v7; |
264 | | #[cfg(feature = "v8")] |
265 | | mod v8; |
266 | | |
267 | | #[cfg(feature = "md5")] |
268 | | mod md5; |
269 | | #[cfg(feature = "rng")] |
270 | | mod rng; |
271 | | #[cfg(feature = "sha1")] |
272 | | mod sha1; |
273 | | |
274 | | mod external; |
275 | | |
276 | | #[doc(hidden)] |
277 | | pub mod __macro_support { |
278 | | pub use crate::std::result::Result::{Err, Ok}; |
279 | | } |
280 | | |
281 | | pub use crate::{builder::Builder, error::Error, non_nil::NonNilUuid}; |
282 | | |
283 | | /// A 128-bit (16 byte) buffer containing the UUID. |
284 | | /// |
285 | | /// # ABI |
286 | | /// |
287 | | /// The `Bytes` type is always guaranteed to be have the same ABI as [`Uuid`]. |
288 | | pub type Bytes = [u8; 16]; |
289 | | |
290 | | /// The version of the UUID, denoting the generating algorithm. |
291 | | /// |
292 | | /// # References |
293 | | /// |
294 | | /// * [Version Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.2) |
295 | | #[derive(Clone, Copy, Debug, PartialEq)] |
296 | | #[non_exhaustive] |
297 | | #[repr(u8)] |
298 | | pub enum Version { |
299 | | /// The "nil" (all zeros) UUID. |
300 | | Nil = 0u8, |
301 | | /// Version 1: Timestamp and node ID. |
302 | | Mac = 1, |
303 | | /// Version 2: DCE Security. |
304 | | Dce = 2, |
305 | | /// Version 3: MD5 hash. |
306 | | Md5 = 3, |
307 | | /// Version 4: Random. |
308 | | Random = 4, |
309 | | /// Version 5: SHA-1 hash. |
310 | | Sha1 = 5, |
311 | | /// Version 6: Sortable Timestamp and node ID. |
312 | | SortMac = 6, |
313 | | /// Version 7: Timestamp and random. |
314 | | SortRand = 7, |
315 | | /// Version 8: Custom. |
316 | | Custom = 8, |
317 | | /// The "max" (all ones) UUID. |
318 | | Max = 0x0f, |
319 | | } |
320 | | |
321 | | /// The reserved variants of UUIDs. |
322 | | /// |
323 | | /// Unlike the version field, which is a strict set of values, the variant |
324 | | /// behaves more like a mask. Multiple bit patterns in a UUID's variant field may correspond |
325 | | /// to the same variant value. |
326 | | /// |
327 | | /// # References |
328 | | /// |
329 | | /// * [Variant Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.1) |
330 | | #[derive(Clone, Copy, Debug, PartialEq)] |
331 | | #[non_exhaustive] |
332 | | #[repr(u8)] |
333 | | pub enum Variant { |
334 | | /// Reserved by the NCS for backward compatibility. |
335 | | /// |
336 | | /// The Nil UUID will return this variant. |
337 | | NCS = 0u8, |
338 | | /// The variant specified in RFC9562. |
339 | | /// |
340 | | /// The majority of UUIDs use this variant. |
341 | | RFC4122, |
342 | | /// Reserved by Microsoft for backward compatibility. |
343 | | Microsoft, |
344 | | /// Reserved for future expansion. |
345 | | /// |
346 | | /// The Max UUID will return this variant. |
347 | | Future, |
348 | | } |
349 | | |
350 | | /// A Universally Unique Identifier (UUID). |
351 | | /// |
352 | | /// # Examples |
353 | | /// |
354 | | /// Parse a UUID given in the simple format and print it as a urn: |
355 | | /// |
356 | | /// ``` |
357 | | /// # use uuid::Uuid; |
358 | | /// # fn main() -> Result<(), uuid::Error> { |
359 | | /// let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?; |
360 | | /// |
361 | | /// println!("{}", my_uuid.urn()); |
362 | | /// # Ok(()) |
363 | | /// # } |
364 | | /// ``` |
365 | | /// |
366 | | /// Create a new random (V4) UUID and print it out in hexadecimal form: |
367 | | /// |
368 | | /// ``` |
369 | | /// // Note that this requires the `v4` feature enabled in the uuid crate. |
370 | | /// # use uuid::Uuid; |
371 | | /// # fn main() { |
372 | | /// # #[cfg(feature = "v4")] { |
373 | | /// let my_uuid = Uuid::new_v4(); |
374 | | /// |
375 | | /// println!("{}", my_uuid); |
376 | | /// # } |
377 | | /// # } |
378 | | /// ``` |
379 | | /// |
380 | | /// # Formatting |
381 | | /// |
382 | | /// A UUID can be formatted in one of a few ways: |
383 | | /// |
384 | | /// * [`simple`](#method.simple): `a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8`. |
385 | | /// * [`hyphenated`](#method.hyphenated): |
386 | | /// `a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8`. |
387 | | /// * [`urn`](#method.urn): `urn:uuid:A1A2A3A4-B1B2-C1C2-D1D2-D3D4D5D6D7D8`. |
388 | | /// * [`braced`](#method.braced): `{a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8}`. |
389 | | /// |
390 | | /// The default representation when formatting a UUID with `Display` is |
391 | | /// hyphenated: |
392 | | /// |
393 | | /// ``` |
394 | | /// # use uuid::Uuid; |
395 | | /// # fn main() -> Result<(), uuid::Error> { |
396 | | /// let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?; |
397 | | /// |
398 | | /// assert_eq!( |
399 | | /// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", |
400 | | /// my_uuid.to_string(), |
401 | | /// ); |
402 | | /// # Ok(()) |
403 | | /// # } |
404 | | /// ``` |
405 | | /// |
406 | | /// Other formats can be specified using adapter methods on the UUID: |
407 | | /// |
408 | | /// ``` |
409 | | /// # use uuid::Uuid; |
410 | | /// # fn main() -> Result<(), uuid::Error> { |
411 | | /// let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?; |
412 | | /// |
413 | | /// assert_eq!( |
414 | | /// "urn:uuid:a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8", |
415 | | /// my_uuid.urn().to_string(), |
416 | | /// ); |
417 | | /// # Ok(()) |
418 | | /// # } |
419 | | /// ``` |
420 | | /// |
421 | | /// # Endianness |
422 | | /// |
423 | | /// The specification for UUIDs encodes the integer fields that make up the |
424 | | /// value in big-endian order. This crate assumes integer inputs are already in |
425 | | /// the correct order by default, regardless of the endianness of the |
426 | | /// environment. Most methods that accept integers have a `_le` variant (such as |
427 | | /// `from_fields_le`) that assumes any integer values will need to have their |
428 | | /// bytes flipped, regardless of the endianness of the environment. |
429 | | /// |
430 | | /// Most users won't need to worry about endianness unless they need to operate |
431 | | /// on individual fields (such as when converting between Microsoft GUIDs). The |
432 | | /// important things to remember are: |
433 | | /// |
434 | | /// - The endianness is in terms of the fields of the UUID, not the environment. |
435 | | /// - The endianness is assumed to be big-endian when there's no `_le` suffix |
436 | | /// somewhere. |
437 | | /// - Byte-flipping in `_le` methods applies to each integer. |
438 | | /// - Endianness roundtrips, so if you create a UUID with `from_fields_le` |
439 | | /// you'll get the same values back out with `to_fields_le`. |
440 | | /// |
441 | | /// # ABI |
442 | | /// |
443 | | /// The `Uuid` type is always guaranteed to be have the same ABI as [`Bytes`]. |
444 | | #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] |
445 | | #[repr(transparent)] |
446 | | // NOTE: Also check `NonNilUuid` when ading new derives here |
447 | | #[cfg_attr( |
448 | | feature = "borsh", |
449 | | derive(borsh_derive::BorshDeserialize, borsh_derive::BorshSerialize) |
450 | | )] |
451 | | #[cfg_attr( |
452 | | feature = "bytemuck", |
453 | | derive(bytemuck::Zeroable, bytemuck::Pod, bytemuck::TransparentWrapper) |
454 | | )] |
455 | | #[cfg_attr( |
456 | | all(uuid_unstable, feature = "zerocopy"), |
457 | | derive( |
458 | | zerocopy::IntoBytes, |
459 | | zerocopy::FromBytes, |
460 | | zerocopy::KnownLayout, |
461 | | zerocopy::Immutable, |
462 | | zerocopy::Unaligned |
463 | | ) |
464 | | )] |
465 | | pub struct Uuid(Bytes); |
466 | | |
467 | | impl Uuid { |
468 | | /// UUID namespace for Domain Name System (DNS). |
469 | | pub const NAMESPACE_DNS: Self = Uuid([ |
470 | | 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, |
471 | | 0xc8, |
472 | | ]); |
473 | | |
474 | | /// UUID namespace for ISO Object Identifiers (OIDs). |
475 | | pub const NAMESPACE_OID: Self = Uuid([ |
476 | | 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, |
477 | | 0xc8, |
478 | | ]); |
479 | | |
480 | | /// UUID namespace for Uniform Resource Locators (URLs). |
481 | | pub const NAMESPACE_URL: Self = Uuid([ |
482 | | 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, |
483 | | 0xc8, |
484 | | ]); |
485 | | |
486 | | /// UUID namespace for X.500 Distinguished Names (DNs). |
487 | | pub const NAMESPACE_X500: Self = Uuid([ |
488 | | 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, |
489 | | 0xc8, |
490 | | ]); |
491 | | |
492 | | /// Returns the variant of the UUID structure. |
493 | | /// |
494 | | /// This determines the interpretation of the structure of the UUID. |
495 | | /// This method simply reads the value of the variant byte. It doesn't |
496 | | /// validate the rest of the UUID as conforming to that variant. |
497 | | /// |
498 | | /// # Examples |
499 | | /// |
500 | | /// Basic usage: |
501 | | /// |
502 | | /// ``` |
503 | | /// # use uuid::{Uuid, Variant}; |
504 | | /// # fn main() -> Result<(), uuid::Error> { |
505 | | /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?; |
506 | | /// |
507 | | /// assert_eq!(Variant::RFC4122, my_uuid.get_variant()); |
508 | | /// # Ok(()) |
509 | | /// # } |
510 | | /// ``` |
511 | | /// |
512 | | /// # References |
513 | | /// |
514 | | /// * [Variant Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.1) |
515 | 0 | pub const fn get_variant(&self) -> Variant { |
516 | 0 | match self.as_bytes()[8] { |
517 | 0 | x if x & 0x80 == 0x00 => Variant::NCS, |
518 | 0 | x if x & 0xc0 == 0x80 => Variant::RFC4122, |
519 | 0 | x if x & 0xe0 == 0xc0 => Variant::Microsoft, |
520 | 0 | x if x & 0xe0 == 0xe0 => Variant::Future, |
521 | | // The above match arms are actually exhaustive |
522 | | // We just return `Future` here because we can't |
523 | | // use `unreachable!()` in a `const fn` |
524 | 0 | _ => Variant::Future, |
525 | | } |
526 | 0 | } |
527 | | |
528 | | /// Returns the version number of the UUID. |
529 | | /// |
530 | | /// This represents the algorithm used to generate the value. |
531 | | /// This method is the future-proof alternative to [`Uuid::get_version`]. |
532 | | /// |
533 | | /// # Examples |
534 | | /// |
535 | | /// Basic usage: |
536 | | /// |
537 | | /// ``` |
538 | | /// # use uuid::Uuid; |
539 | | /// # fn main() -> Result<(), uuid::Error> { |
540 | | /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?; |
541 | | /// |
542 | | /// assert_eq!(3, my_uuid.get_version_num()); |
543 | | /// # Ok(()) |
544 | | /// # } |
545 | | /// ``` |
546 | | /// |
547 | | /// # References |
548 | | /// |
549 | | /// * [Version Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.2) |
550 | 0 | pub const fn get_version_num(&self) -> usize { |
551 | 0 | (self.as_bytes()[6] >> 4) as usize |
552 | 0 | } |
553 | | |
554 | | /// Returns the version of the UUID. |
555 | | /// |
556 | | /// This represents the algorithm used to generate the value. |
557 | | /// If the version field doesn't contain a recognized version then `None` |
558 | | /// is returned. If you're trying to read the version for a future extension |
559 | | /// you can also use [`Uuid::get_version_num`] to unconditionally return a |
560 | | /// number. Future extensions may start to return `Some` once they're |
561 | | /// standardized and supported. |
562 | | /// |
563 | | /// # Examples |
564 | | /// |
565 | | /// Basic usage: |
566 | | /// |
567 | | /// ``` |
568 | | /// # use uuid::{Uuid, Version}; |
569 | | /// # fn main() -> Result<(), uuid::Error> { |
570 | | /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?; |
571 | | /// |
572 | | /// assert_eq!(Some(Version::Md5), my_uuid.get_version()); |
573 | | /// # Ok(()) |
574 | | /// # } |
575 | | /// ``` |
576 | | /// |
577 | | /// # References |
578 | | /// |
579 | | /// * [Version Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.2) |
580 | 0 | pub const fn get_version(&self) -> Option<Version> { |
581 | 0 | match self.get_version_num() { |
582 | 0 | 0 if self.is_nil() => Some(Version::Nil), |
583 | 0 | 1 => Some(Version::Mac), |
584 | 0 | 2 => Some(Version::Dce), |
585 | 0 | 3 => Some(Version::Md5), |
586 | 0 | 4 => Some(Version::Random), |
587 | 0 | 5 => Some(Version::Sha1), |
588 | 0 | 6 => Some(Version::SortMac), |
589 | 0 | 7 => Some(Version::SortRand), |
590 | 0 | 8 => Some(Version::Custom), |
591 | 0 | 0xf if self.is_max() => Some(Version::Max), |
592 | 0 | _ => None, |
593 | | } |
594 | 0 | } |
595 | | |
596 | | /// Returns the four field values of the UUID. |
597 | | /// |
598 | | /// These values can be passed to the [`Uuid::from_fields`] method to get |
599 | | /// the original `Uuid` back. |
600 | | /// |
601 | | /// * The first field value represents the first group of (eight) hex |
602 | | /// digits, taken as a big-endian `u32` value. For V1 UUIDs, this field |
603 | | /// represents the low 32 bits of the timestamp. |
604 | | /// * The second field value represents the second group of (four) hex |
605 | | /// digits, taken as a big-endian `u16` value. For V1 UUIDs, this field |
606 | | /// represents the middle 16 bits of the timestamp. |
607 | | /// * The third field value represents the third group of (four) hex digits, |
608 | | /// taken as a big-endian `u16` value. The 4 most significant bits give |
609 | | /// the UUID version, and for V1 UUIDs, the last 12 bits represent the |
610 | | /// high 12 bits of the timestamp. |
611 | | /// * The last field value represents the last two groups of four and twelve |
612 | | /// hex digits, taken in order. The first 1-3 bits of this indicate the |
613 | | /// UUID variant, and for V1 UUIDs, the next 13-15 bits indicate the clock |
614 | | /// sequence and the last 48 bits indicate the node ID. |
615 | | /// |
616 | | /// # Examples |
617 | | /// |
618 | | /// ``` |
619 | | /// # use uuid::Uuid; |
620 | | /// # fn main() -> Result<(), uuid::Error> { |
621 | | /// let uuid = Uuid::nil(); |
622 | | /// |
623 | | /// assert_eq!(uuid.as_fields(), (0, 0, 0, &[0u8; 8])); |
624 | | /// |
625 | | /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?; |
626 | | /// |
627 | | /// assert_eq!( |
628 | | /// uuid.as_fields(), |
629 | | /// ( |
630 | | /// 0xa1a2a3a4, |
631 | | /// 0xb1b2, |
632 | | /// 0xc1c2, |
633 | | /// &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8], |
634 | | /// ) |
635 | | /// ); |
636 | | /// # Ok(()) |
637 | | /// # } |
638 | | /// ``` |
639 | 0 | pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8]) { |
640 | 0 | let bytes = self.as_bytes(); |
641 | | |
642 | 0 | let d1 = (bytes[0] as u32) << 24 |
643 | 0 | | (bytes[1] as u32) << 16 |
644 | 0 | | (bytes[2] as u32) << 8 |
645 | 0 | | (bytes[3] as u32); |
646 | | |
647 | 0 | let d2 = (bytes[4] as u16) << 8 | (bytes[5] as u16); |
648 | | |
649 | 0 | let d3 = (bytes[6] as u16) << 8 | (bytes[7] as u16); |
650 | | |
651 | 0 | let d4: &[u8; 8] = bytes[8..16].try_into().unwrap(); |
652 | 0 | (d1, d2, d3, d4) |
653 | 0 | } |
654 | | |
655 | | /// Returns the four field values of the UUID in little-endian order. |
656 | | /// |
657 | | /// The bytes in the returned integer fields will be converted from |
658 | | /// big-endian order. This is based on the endianness of the UUID, |
659 | | /// rather than the target environment so bytes will be flipped on both |
660 | | /// big and little endian machines. |
661 | | /// |
662 | | /// # Examples |
663 | | /// |
664 | | /// ``` |
665 | | /// use uuid::Uuid; |
666 | | /// |
667 | | /// # fn main() -> Result<(), uuid::Error> { |
668 | | /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?; |
669 | | /// |
670 | | /// assert_eq!( |
671 | | /// uuid.to_fields_le(), |
672 | | /// ( |
673 | | /// 0xa4a3a2a1, |
674 | | /// 0xb2b1, |
675 | | /// 0xc2c1, |
676 | | /// &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8], |
677 | | /// ) |
678 | | /// ); |
679 | | /// # Ok(()) |
680 | | /// # } |
681 | | /// ``` |
682 | 0 | pub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8]) { |
683 | 0 | let d1 = (self.as_bytes()[0] as u32) |
684 | 0 | | (self.as_bytes()[1] as u32) << 8 |
685 | 0 | | (self.as_bytes()[2] as u32) << 16 |
686 | 0 | | (self.as_bytes()[3] as u32) << 24; |
687 | | |
688 | 0 | let d2 = (self.as_bytes()[4] as u16) | (self.as_bytes()[5] as u16) << 8; |
689 | | |
690 | 0 | let d3 = (self.as_bytes()[6] as u16) | (self.as_bytes()[7] as u16) << 8; |
691 | | |
692 | 0 | let d4: &[u8; 8] = self.as_bytes()[8..16].try_into().unwrap(); |
693 | 0 | (d1, d2, d3, d4) |
694 | 0 | } |
695 | | |
696 | | /// Returns a 128bit value containing the value. |
697 | | /// |
698 | | /// The bytes in the UUID will be packed directly into a `u128`. |
699 | | /// |
700 | | /// # Examples |
701 | | /// |
702 | | /// ``` |
703 | | /// # use uuid::Uuid; |
704 | | /// # fn main() -> Result<(), uuid::Error> { |
705 | | /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?; |
706 | | /// |
707 | | /// assert_eq!( |
708 | | /// uuid.as_u128(), |
709 | | /// 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8, |
710 | | /// ); |
711 | | /// # Ok(()) |
712 | | /// # } |
713 | | /// ``` |
714 | 0 | pub const fn as_u128(&self) -> u128 { |
715 | 0 | u128::from_be_bytes(*self.as_bytes()) |
716 | 0 | } |
717 | | |
718 | | /// Returns a 128bit little-endian value containing the value. |
719 | | /// |
720 | | /// The bytes in the `u128` will be flipped to convert into big-endian |
721 | | /// order. This is based on the endianness of the UUID, rather than the |
722 | | /// target environment so bytes will be flipped on both big and little |
723 | | /// endian machines. |
724 | | /// |
725 | | /// Note that this will produce a different result than |
726 | | /// [`Uuid::to_fields_le`], because the entire UUID is reversed, rather |
727 | | /// than reversing the individual fields in-place. |
728 | | /// |
729 | | /// # Examples |
730 | | /// |
731 | | /// ``` |
732 | | /// # use uuid::Uuid; |
733 | | /// # fn main() -> Result<(), uuid::Error> { |
734 | | /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?; |
735 | | /// |
736 | | /// assert_eq!( |
737 | | /// uuid.to_u128_le(), |
738 | | /// 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1, |
739 | | /// ); |
740 | | /// # Ok(()) |
741 | | /// # } |
742 | | /// ``` |
743 | 0 | pub const fn to_u128_le(&self) -> u128 { |
744 | 0 | u128::from_le_bytes(*self.as_bytes()) |
745 | 0 | } |
746 | | |
747 | | /// Returns two 64bit values containing the value. |
748 | | /// |
749 | | /// The bytes in the UUID will be split into two `u64`. |
750 | | /// The first u64 represents the 64 most significant bits, |
751 | | /// the second one represents the 64 least significant. |
752 | | /// |
753 | | /// # Examples |
754 | | /// |
755 | | /// ``` |
756 | | /// # use uuid::Uuid; |
757 | | /// # fn main() -> Result<(), uuid::Error> { |
758 | | /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?; |
759 | | /// assert_eq!( |
760 | | /// uuid.as_u64_pair(), |
761 | | /// (0xa1a2a3a4b1b2c1c2, 0xd1d2d3d4d5d6d7d8), |
762 | | /// ); |
763 | | /// # Ok(()) |
764 | | /// # } |
765 | | /// ``` |
766 | 0 | pub const fn as_u64_pair(&self) -> (u64, u64) { |
767 | 0 | let value = self.as_u128(); |
768 | 0 | ((value >> 64) as u64, value as u64) |
769 | 0 | } |
770 | | |
771 | | /// Returns a slice of 16 octets containing the value. |
772 | | /// |
773 | | /// This method borrows the underlying byte value of the UUID. |
774 | | /// |
775 | | /// # Examples |
776 | | /// |
777 | | /// ``` |
778 | | /// # use uuid::Uuid; |
779 | | /// let bytes1 = [ |
780 | | /// 0xa1, 0xa2, 0xa3, 0xa4, |
781 | | /// 0xb1, 0xb2, |
782 | | /// 0xc1, 0xc2, |
783 | | /// 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, |
784 | | /// ]; |
785 | | /// let uuid1 = Uuid::from_bytes_ref(&bytes1); |
786 | | /// |
787 | | /// let bytes2 = uuid1.as_bytes(); |
788 | | /// let uuid2 = Uuid::from_bytes_ref(bytes2); |
789 | | /// |
790 | | /// assert_eq!(uuid1, uuid2); |
791 | | /// |
792 | | /// assert!(std::ptr::eq( |
793 | | /// uuid2 as *const Uuid as *const u8, |
794 | | /// &bytes1 as *const [u8; 16] as *const u8, |
795 | | /// )); |
796 | | /// ``` |
797 | | #[inline] |
798 | 0 | pub const fn as_bytes(&self) -> &Bytes { |
799 | 0 | &self.0 |
800 | 0 | } |
801 | | |
802 | | /// Consumes self and returns the underlying byte value of the UUID. |
803 | | /// |
804 | | /// # Examples |
805 | | /// |
806 | | /// ``` |
807 | | /// # use uuid::Uuid; |
808 | | /// let bytes = [ |
809 | | /// 0xa1, 0xa2, 0xa3, 0xa4, |
810 | | /// 0xb1, 0xb2, |
811 | | /// 0xc1, 0xc2, |
812 | | /// 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, |
813 | | /// ]; |
814 | | /// let uuid = Uuid::from_bytes(bytes); |
815 | | /// assert_eq!(bytes, uuid.into_bytes()); |
816 | | /// ``` |
817 | | #[inline] |
818 | 0 | pub const fn into_bytes(self) -> Bytes { |
819 | 0 | self.0 |
820 | 0 | } |
821 | | |
822 | | /// Returns the bytes of the UUID in little-endian order. |
823 | | /// |
824 | | /// The bytes for each field will be flipped to convert into little-endian order. |
825 | | /// This is based on the endianness of the UUID, rather than the target environment |
826 | | /// so bytes will be flipped on both big and little endian machines. |
827 | | /// |
828 | | /// Note that ordering is applied to each _field_, rather than to the bytes as a whole. |
829 | | /// This ordering is compatible with Microsoft's mixed endian GUID format. |
830 | | /// |
831 | | /// # Examples |
832 | | /// |
833 | | /// ``` |
834 | | /// use uuid::Uuid; |
835 | | /// |
836 | | /// # fn main() -> Result<(), uuid::Error> { |
837 | | /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?; |
838 | | /// |
839 | | /// assert_eq!( |
840 | | /// uuid.to_bytes_le(), |
841 | | /// ([ |
842 | | /// 0xa4, 0xa3, 0xa2, 0xa1, 0xb2, 0xb1, 0xc2, 0xc1, 0xd1, 0xd2, |
843 | | /// 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8 |
844 | | /// ]) |
845 | | /// ); |
846 | | /// # Ok(()) |
847 | | /// # } |
848 | | /// ``` |
849 | 0 | pub const fn to_bytes_le(&self) -> Bytes { |
850 | 0 | [ |
851 | 0 | self.0[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4], self.0[7], self.0[6], |
852 | 0 | self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14], |
853 | 0 | self.0[15], |
854 | 0 | ] |
855 | 0 | } |
856 | | |
857 | | /// Tests if the UUID is nil (all zeros). |
858 | 0 | pub const fn is_nil(&self) -> bool { |
859 | 0 | self.as_u128() == u128::MIN |
860 | 0 | } |
861 | | |
862 | | /// Tests if the UUID is max (all ones). |
863 | 0 | pub const fn is_max(&self) -> bool { |
864 | 0 | self.as_u128() == u128::MAX |
865 | 0 | } |
866 | | |
867 | | /// A buffer that can be used for `encode_...` calls, that is |
868 | | /// guaranteed to be long enough for any of the format adapters. |
869 | | /// |
870 | | /// # Examples |
871 | | /// |
872 | | /// ``` |
873 | | /// # use uuid::Uuid; |
874 | | /// let uuid = Uuid::nil(); |
875 | | /// |
876 | | /// assert_eq!( |
877 | | /// uuid.simple().encode_lower(&mut Uuid::encode_buffer()), |
878 | | /// "00000000000000000000000000000000" |
879 | | /// ); |
880 | | /// |
881 | | /// assert_eq!( |
882 | | /// uuid.hyphenated() |
883 | | /// .encode_lower(&mut Uuid::encode_buffer()), |
884 | | /// "00000000-0000-0000-0000-000000000000" |
885 | | /// ); |
886 | | /// |
887 | | /// assert_eq!( |
888 | | /// uuid.urn().encode_lower(&mut Uuid::encode_buffer()), |
889 | | /// "urn:uuid:00000000-0000-0000-0000-000000000000" |
890 | | /// ); |
891 | | /// ``` |
892 | 0 | pub const fn encode_buffer() -> [u8; fmt::Urn::LENGTH] { |
893 | 0 | [0; fmt::Urn::LENGTH] |
894 | 0 | } |
895 | | |
896 | | /// If the UUID is the correct version (v1, v6, or v7) this will return |
897 | | /// the timestamp in a version-agnostic [`Timestamp`]. For other versions |
898 | | /// this will return `None`. |
899 | | /// |
900 | | /// # Roundtripping |
901 | | /// |
902 | | /// This method is unlikely to roundtrip a timestamp in a UUID due to the way |
903 | | /// UUIDs encode timestamps. The timestamp returned from this method will be truncated to |
904 | | /// 100ns precision for version 1 and 6 UUIDs, and to millisecond precision for version 7 UUIDs. |
905 | 0 | pub const fn get_timestamp(&self) -> Option<Timestamp> { |
906 | 0 | match self.get_version() { |
907 | | Some(Version::Mac) => { |
908 | 0 | let (ticks, counter) = timestamp::decode_gregorian_timestamp(self); |
909 | | |
910 | 0 | Some(Timestamp::from_gregorian_time(ticks, counter)) |
911 | | } |
912 | | Some(Version::SortMac) => { |
913 | 0 | let (ticks, counter) = timestamp::decode_sorted_gregorian_timestamp(self); |
914 | | |
915 | 0 | Some(Timestamp::from_gregorian_time(ticks, counter)) |
916 | | } |
917 | | Some(Version::SortRand) => { |
918 | 0 | let millis = timestamp::decode_unix_timestamp_millis(self); |
919 | | |
920 | 0 | let seconds = millis / 1000; |
921 | 0 | let nanos = ((millis % 1000) * 1_000_000) as u32; |
922 | | |
923 | 0 | Some(Timestamp::from_unix_time(seconds, nanos, 0, 0)) |
924 | | } |
925 | 0 | _ => None, |
926 | | } |
927 | 0 | } |
928 | | |
929 | | /// If the UUID is the correct version (v1, or v6) this will return the |
930 | | /// node value as a 6-byte array. For other versions this will return `None`. |
931 | 0 | pub const fn get_node_id(&self) -> Option<[u8; 6]> { |
932 | 0 | match self.get_version() { |
933 | | Some(Version::Mac) | Some(Version::SortMac) => { |
934 | 0 | let mut node_id = [0; 6]; |
935 | | |
936 | 0 | node_id[0] = self.0[10]; |
937 | 0 | node_id[1] = self.0[11]; |
938 | 0 | node_id[2] = self.0[12]; |
939 | 0 | node_id[3] = self.0[13]; |
940 | 0 | node_id[4] = self.0[14]; |
941 | 0 | node_id[5] = self.0[15]; |
942 | | |
943 | 0 | Some(node_id) |
944 | | } |
945 | 0 | _ => None, |
946 | | } |
947 | 0 | } |
948 | | } |
949 | | |
950 | | impl Hash for Uuid { |
951 | 0 | fn hash<H: Hasher>(&self, state: &mut H) { |
952 | 0 | state.write(&self.0); |
953 | 0 | } |
954 | | } |
955 | | |
956 | | impl Default for Uuid { |
957 | | #[inline] |
958 | 0 | fn default() -> Self { |
959 | 0 | Uuid::nil() |
960 | 0 | } |
961 | | } |
962 | | |
963 | | impl AsRef<Uuid> for Uuid { |
964 | | #[inline] |
965 | 0 | fn as_ref(&self) -> &Uuid { |
966 | 0 | self |
967 | 0 | } |
968 | | } |
969 | | |
970 | | impl AsRef<[u8]> for Uuid { |
971 | | #[inline] |
972 | 0 | fn as_ref(&self) -> &[u8] { |
973 | 0 | &self.0 |
974 | 0 | } |
975 | | } |
976 | | |
977 | | #[cfg(feature = "std")] |
978 | | impl From<Uuid> for std::vec::Vec<u8> { |
979 | 0 | fn from(value: Uuid) -> Self { |
980 | 0 | value.0.to_vec() |
981 | 0 | } |
982 | | } |
983 | | |
984 | | #[cfg(feature = "std")] |
985 | | impl TryFrom<std::vec::Vec<u8>> for Uuid { |
986 | | type Error = Error; |
987 | | |
988 | 0 | fn try_from(value: std::vec::Vec<u8>) -> Result<Self, Self::Error> { |
989 | 0 | Uuid::from_slice(&value) |
990 | 0 | } |
991 | | } |
992 | | |
993 | | #[cfg(feature = "serde")] |
994 | | pub mod serde { |
995 | | //! Adapters for alternative `serde` formats. |
996 | | //! |
997 | | //! This module contains adapters you can use with [`#[serde(with)]`](https://serde.rs/field-attrs.html#with) |
998 | | //! to change the way a [`Uuid`](../struct.Uuid.html) is serialized |
999 | | //! and deserialized. |
1000 | | |
1001 | | pub use crate::external::serde_support::{braced, compact, hyphenated, simple, urn}; |
1002 | | } |
1003 | | |
1004 | | #[cfg(test)] |
1005 | | mod tests { |
1006 | | use super::*; |
1007 | | |
1008 | | use crate::std::string::{String, ToString}; |
1009 | | |
1010 | | #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] |
1011 | | use wasm_bindgen_test::*; |
1012 | | |
1013 | | macro_rules! check { |
1014 | | ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => { |
1015 | | $buf.clear(); |
1016 | | write!($buf, $format, $target).unwrap(); |
1017 | | assert!($buf.len() == $len); |
1018 | | assert!($buf.chars().all($cond), "{}", $buf); |
1019 | | |
1020 | | assert_eq!(Uuid::parse_str(&$buf).unwrap(), $target); |
1021 | | }; |
1022 | | } |
1023 | | |
1024 | | pub fn some_uuid_nil() -> Uuid { |
1025 | | Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap() |
1026 | | } |
1027 | | |
1028 | | pub fn some_uuid_v1() -> Uuid { |
1029 | | Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap() |
1030 | | } |
1031 | | |
1032 | | pub fn some_uuid_v3() -> Uuid { |
1033 | | Uuid::parse_str("bcee7a9c-52f1-30c6-a3cc-8c72ba634990").unwrap() |
1034 | | } |
1035 | | |
1036 | | pub fn some_uuid_v4() -> Uuid { |
1037 | | Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap() |
1038 | | } |
1039 | | |
1040 | | pub fn some_uuid_v4_2() -> Uuid { |
1041 | | Uuid::parse_str("c0dd0820-b35a-4c56-bc7d-0f0b04241adb").unwrap() |
1042 | | } |
1043 | | |
1044 | | pub fn some_uuid_v5() -> Uuid { |
1045 | | Uuid::parse_str("b11f79a5-1e6d-57ce-a4b5-ba8531ea03d0").unwrap() |
1046 | | } |
1047 | | |
1048 | | pub fn some_uuid_v6() -> Uuid { |
1049 | | Uuid::parse_str("1e74ba22-0616-6934-8000-010203040506").unwrap() |
1050 | | } |
1051 | | |
1052 | | pub fn some_uuid_v7() -> Uuid { |
1053 | | Uuid::parse_str("015c837b-9e84-7db5-b059-c75a84585688").unwrap() |
1054 | | } |
1055 | | |
1056 | | pub fn some_uuid_v8() -> Uuid { |
1057 | | Uuid::parse_str("0f0e0d0c-0b0a-8908-8706-050403020100").unwrap() |
1058 | | } |
1059 | | |
1060 | | pub fn some_uuid_max() -> Uuid { |
1061 | | Uuid::parse_str("ffffffff-ffff-ffff-ffff-ffffffffffff").unwrap() |
1062 | | } |
1063 | | |
1064 | | pub fn some_uuid_iter() -> impl Iterator<Item = Uuid> { |
1065 | | [ |
1066 | | some_uuid_nil(), |
1067 | | some_uuid_v1(), |
1068 | | some_uuid_v3(), |
1069 | | some_uuid_v4(), |
1070 | | some_uuid_v5(), |
1071 | | some_uuid_v6(), |
1072 | | some_uuid_v7(), |
1073 | | some_uuid_v8(), |
1074 | | some_uuid_max(), |
1075 | | ] |
1076 | | .into_iter() |
1077 | | } |
1078 | | |
1079 | | pub fn some_uuid_v_iter() -> impl Iterator<Item = Uuid> { |
1080 | | [ |
1081 | | some_uuid_v1(), |
1082 | | some_uuid_v3(), |
1083 | | some_uuid_v4(), |
1084 | | some_uuid_v5(), |
1085 | | some_uuid_v6(), |
1086 | | some_uuid_v7(), |
1087 | | some_uuid_v8(), |
1088 | | ] |
1089 | | .into_iter() |
1090 | | } |
1091 | | |
1092 | | #[test] |
1093 | | #[cfg_attr( |
1094 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1095 | | wasm_bindgen_test |
1096 | | )] |
1097 | | #[cfg(feature = "std")] |
1098 | | fn test_compare() { |
1099 | | use std::{ |
1100 | | cmp::Ordering, |
1101 | | hash::{BuildHasher, BuildHasherDefault, DefaultHasher}, |
1102 | | }; |
1103 | | |
1104 | | let a = some_uuid_v4(); |
1105 | | let b = some_uuid_v4_2(); |
1106 | | |
1107 | | let ah = BuildHasherDefault::<DefaultHasher>::default().hash_one(a); |
1108 | | let bh = BuildHasherDefault::<DefaultHasher>::default().hash_one(b); |
1109 | | |
1110 | | assert_eq!(a, a); |
1111 | | assert_eq!(b, b); |
1112 | | assert_eq!(Ordering::Equal, a.cmp(&a)); |
1113 | | assert_eq!(Ordering::Equal, b.cmp(&b)); |
1114 | | |
1115 | | assert_ne!(a, b); |
1116 | | assert_ne!(b, a); |
1117 | | assert_ne!(Ordering::Equal, b.cmp(&a)); |
1118 | | assert_ne!(Ordering::Equal, a.cmp(&b)); |
1119 | | assert_ne!(ah, bh); |
1120 | | } |
1121 | | |
1122 | | #[test] |
1123 | | #[cfg_attr( |
1124 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1125 | | wasm_bindgen_test |
1126 | | )] |
1127 | | fn test_default() { |
1128 | | let default_uuid = Uuid::default(); |
1129 | | let nil_uuid = Uuid::nil(); |
1130 | | |
1131 | | assert_eq!(default_uuid, nil_uuid); |
1132 | | } |
1133 | | |
1134 | | #[test] |
1135 | | #[cfg_attr( |
1136 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1137 | | wasm_bindgen_test |
1138 | | )] |
1139 | | fn test_display() { |
1140 | | use crate::std::fmt::Write; |
1141 | | |
1142 | | for uuid in some_uuid_iter() { |
1143 | | let s = uuid.to_string(); |
1144 | | let mut buffer = String::new(); |
1145 | | |
1146 | | assert_eq!(s, uuid.hyphenated().to_string()); |
1147 | | |
1148 | | check!(buffer, "{}", some_uuid_v4(), 36, |c| c.is_lowercase() |
1149 | | || c.is_ascii_digit() |
1150 | | || c == '-'); |
1151 | | } |
1152 | | } |
1153 | | |
1154 | | #[test] |
1155 | | #[cfg_attr( |
1156 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1157 | | wasm_bindgen_test |
1158 | | )] |
1159 | | fn test_to_simple_string() { |
1160 | | for uuid in some_uuid_iter() { |
1161 | | let s = uuid.simple().to_string(); |
1162 | | |
1163 | | assert_eq!(s.len(), 32); |
1164 | | assert!(s.chars().all(|c| c.is_ascii_hexdigit())); |
1165 | | |
1166 | | assert_eq!(Uuid::parse_str(&s).unwrap(), uuid); |
1167 | | } |
1168 | | } |
1169 | | |
1170 | | #[test] |
1171 | | #[cfg_attr( |
1172 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1173 | | wasm_bindgen_test |
1174 | | )] |
1175 | | fn test_hyphenated_string() { |
1176 | | for uuid in some_uuid_iter() { |
1177 | | let s = uuid.hyphenated().to_string(); |
1178 | | |
1179 | | assert_eq!(36, s.len()); |
1180 | | assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-')); |
1181 | | |
1182 | | assert_eq!(Uuid::parse_str(&s).unwrap(), uuid); |
1183 | | } |
1184 | | } |
1185 | | |
1186 | | #[test] |
1187 | | #[cfg_attr( |
1188 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1189 | | wasm_bindgen_test |
1190 | | )] |
1191 | | fn test_upper_lower_hex() { |
1192 | | use std::fmt::Write; |
1193 | | |
1194 | | let mut buf = String::new(); |
1195 | | |
1196 | | macro_rules! check { |
1197 | | ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => { |
1198 | | $buf.clear(); |
1199 | | write!($buf, $format, $target).unwrap(); |
1200 | | assert_eq!($len, buf.len()); |
1201 | | assert!($buf.chars().all($cond), "{}", $buf); |
1202 | | }; |
1203 | | } |
1204 | | |
1205 | | for uuid in some_uuid_iter() { |
1206 | | check!(buf, "{:x}", uuid, 36, |c| c.is_lowercase() |
1207 | | || c.is_ascii_digit() |
1208 | | || c == '-'); |
1209 | | check!(buf, "{:X}", uuid, 36, |c| c.is_uppercase() |
1210 | | || c.is_ascii_digit() |
1211 | | || c == '-'); |
1212 | | check!(buf, "{:#x}", uuid, 36, |c| c.is_lowercase() |
1213 | | || c.is_ascii_digit() |
1214 | | || c == '-'); |
1215 | | check!(buf, "{:#X}", uuid, 36, |c| c.is_uppercase() |
1216 | | || c.is_ascii_digit() |
1217 | | || c == '-'); |
1218 | | |
1219 | | check!(buf, "{:X}", uuid.hyphenated(), 36, |c| c.is_uppercase() |
1220 | | || c.is_ascii_digit() |
1221 | | || c == '-'); |
1222 | | check!(buf, "{:X}", uuid.simple(), 32, |c| c.is_uppercase() |
1223 | | || c.is_ascii_digit()); |
1224 | | check!(buf, "{:#X}", uuid.hyphenated(), 36, |c| c.is_uppercase() |
1225 | | || c.is_ascii_digit() |
1226 | | || c == '-'); |
1227 | | check!(buf, "{:#X}", uuid.simple(), 32, |c| c.is_uppercase() |
1228 | | || c.is_ascii_digit()); |
1229 | | |
1230 | | check!(buf, "{:x}", uuid.hyphenated(), 36, |c| c.is_lowercase() |
1231 | | || c.is_ascii_digit() |
1232 | | || c == '-'); |
1233 | | check!(buf, "{:x}", uuid.simple(), 32, |c| c.is_lowercase() |
1234 | | || c.is_ascii_digit()); |
1235 | | check!(buf, "{:#x}", uuid.hyphenated(), 36, |c| c.is_lowercase() |
1236 | | || c.is_ascii_digit() |
1237 | | || c == '-'); |
1238 | | check!(buf, "{:#x}", uuid.simple(), 32, |c| c.is_lowercase() |
1239 | | || c.is_ascii_digit()); |
1240 | | } |
1241 | | } |
1242 | | |
1243 | | #[test] |
1244 | | #[cfg_attr( |
1245 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1246 | | wasm_bindgen_test |
1247 | | )] |
1248 | | fn test_to_urn_string() { |
1249 | | for uuid in some_uuid_iter() { |
1250 | | let ss = uuid.urn().to_string(); |
1251 | | let s = &ss[9..]; |
1252 | | |
1253 | | assert!(ss.starts_with("urn:uuid:")); |
1254 | | assert_eq!(s.len(), 36); |
1255 | | assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-')); |
1256 | | |
1257 | | assert_eq!(Uuid::parse_str(&ss).unwrap(), uuid); |
1258 | | } |
1259 | | } |
1260 | | |
1261 | | #[test] |
1262 | | #[cfg_attr( |
1263 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1264 | | wasm_bindgen_test |
1265 | | )] |
1266 | | fn test_nil() { |
1267 | | let nil = Uuid::nil(); |
1268 | | let not_nil = some_uuid_v4(); |
1269 | | |
1270 | | assert!(nil.is_nil()); |
1271 | | assert!(!not_nil.is_nil()); |
1272 | | |
1273 | | assert_eq!(nil.get_version(), Some(Version::Nil)); |
1274 | | assert_eq!(nil.get_variant(), Variant::NCS); |
1275 | | |
1276 | | assert_eq!(not_nil.get_version(), Some(Version::Random)); |
1277 | | |
1278 | | assert_eq!( |
1279 | | nil, |
1280 | | Builder::from_bytes([0; 16]) |
1281 | | .with_version(Version::Nil) |
1282 | | .into_uuid() |
1283 | | ); |
1284 | | } |
1285 | | |
1286 | | #[test] |
1287 | | #[cfg_attr( |
1288 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1289 | | wasm_bindgen_test |
1290 | | )] |
1291 | | fn test_max() { |
1292 | | let max = Uuid::max(); |
1293 | | let not_max = some_uuid_v4(); |
1294 | | |
1295 | | assert!(max.is_max()); |
1296 | | assert!(!not_max.is_max()); |
1297 | | |
1298 | | assert_eq!(max.get_version(), Some(Version::Max)); |
1299 | | assert_eq!(max.get_variant(), Variant::Future); |
1300 | | |
1301 | | assert_eq!(not_max.get_version(), Some(Version::Random)); |
1302 | | |
1303 | | assert_eq!( |
1304 | | max, |
1305 | | Builder::from_bytes([0xff; 16]) |
1306 | | .with_version(Version::Max) |
1307 | | .into_uuid() |
1308 | | ); |
1309 | | } |
1310 | | |
1311 | | #[test] |
1312 | | #[cfg_attr( |
1313 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1314 | | wasm_bindgen_test |
1315 | | )] |
1316 | | fn test_predefined_namespaces() { |
1317 | | assert_eq!( |
1318 | | Uuid::NAMESPACE_DNS.hyphenated().to_string(), |
1319 | | "6ba7b810-9dad-11d1-80b4-00c04fd430c8" |
1320 | | ); |
1321 | | assert_eq!( |
1322 | | Uuid::NAMESPACE_URL.hyphenated().to_string(), |
1323 | | "6ba7b811-9dad-11d1-80b4-00c04fd430c8" |
1324 | | ); |
1325 | | assert_eq!( |
1326 | | Uuid::NAMESPACE_OID.hyphenated().to_string(), |
1327 | | "6ba7b812-9dad-11d1-80b4-00c04fd430c8" |
1328 | | ); |
1329 | | assert_eq!( |
1330 | | Uuid::NAMESPACE_X500.hyphenated().to_string(), |
1331 | | "6ba7b814-9dad-11d1-80b4-00c04fd430c8" |
1332 | | ); |
1333 | | } |
1334 | | |
1335 | | #[test] |
1336 | | #[cfg_attr( |
1337 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1338 | | wasm_bindgen_test |
1339 | | )] |
1340 | | fn test_get_timestamp_unsupported_version() { |
1341 | | for uuid in [ |
1342 | | some_uuid_nil(), |
1343 | | some_uuid_v3(), |
1344 | | some_uuid_v4(), |
1345 | | some_uuid_v5(), |
1346 | | some_uuid_v8(), |
1347 | | some_uuid_max(), |
1348 | | ] { |
1349 | | assert_ne!(Version::Mac, uuid.get_version().unwrap()); |
1350 | | assert_ne!(Version::SortMac, uuid.get_version().unwrap()); |
1351 | | assert_ne!(Version::SortRand, uuid.get_version().unwrap()); |
1352 | | |
1353 | | assert!(uuid.get_timestamp().is_none()); |
1354 | | } |
1355 | | } |
1356 | | |
1357 | | #[test] |
1358 | | #[cfg_attr( |
1359 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1360 | | wasm_bindgen_test |
1361 | | )] |
1362 | | fn test_get_node_id_unsupported_version() { |
1363 | | for uuid in [ |
1364 | | some_uuid_nil(), |
1365 | | some_uuid_v4(), |
1366 | | some_uuid_v7(), |
1367 | | some_uuid_v8(), |
1368 | | some_uuid_max(), |
1369 | | ] { |
1370 | | assert_ne!(Version::Mac, uuid.get_version().unwrap()); |
1371 | | assert_ne!(Version::SortMac, uuid.get_version().unwrap()); |
1372 | | |
1373 | | assert!(uuid.get_node_id().is_none()); |
1374 | | } |
1375 | | } |
1376 | | |
1377 | | #[test] |
1378 | | #[cfg_attr( |
1379 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1380 | | wasm_bindgen_test |
1381 | | )] |
1382 | | fn test_get_version() { |
1383 | | fn assert_version(uuid: Uuid, expected: Version) { |
1384 | | assert_eq!( |
1385 | | uuid.get_version().unwrap(), |
1386 | | expected, |
1387 | | "{uuid} version doesn't match {expected:?}" |
1388 | | ); |
1389 | | assert_eq!( |
1390 | | uuid.get_version_num(), |
1391 | | expected as usize, |
1392 | | "{uuid} version doesn't match {}", |
1393 | | expected as usize |
1394 | | ); |
1395 | | } |
1396 | | |
1397 | | assert_version(some_uuid_nil(), Version::Nil); |
1398 | | assert_version(some_uuid_v1(), Version::Mac); |
1399 | | assert_version(some_uuid_v3(), Version::Md5); |
1400 | | assert_version(some_uuid_v4(), Version::Random); |
1401 | | assert_version(some_uuid_v5(), Version::Sha1); |
1402 | | assert_version(some_uuid_v6(), Version::SortMac); |
1403 | | assert_version(some_uuid_v7(), Version::SortRand); |
1404 | | assert_version(some_uuid_v8(), Version::Custom); |
1405 | | assert_version(some_uuid_max(), Version::Max); |
1406 | | } |
1407 | | |
1408 | | #[test] |
1409 | | #[cfg_attr( |
1410 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1411 | | wasm_bindgen_test |
1412 | | )] |
1413 | | fn test_get_version_non_conforming() { |
1414 | | for case in [ |
1415 | | Uuid::from_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]), |
1416 | | Uuid::parse_str("00000000-0000-0000-0000-00000000000f").unwrap(), |
1417 | | Uuid::parse_str("ffffffff-ffff-ffff-ffff-fffffffffff0").unwrap(), |
1418 | | ] { |
1419 | | assert_eq!(case.get_version(), None); |
1420 | | } |
1421 | | } |
1422 | | |
1423 | | #[test] |
1424 | | #[cfg_attr( |
1425 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1426 | | wasm_bindgen_test |
1427 | | )] |
1428 | | fn test_get_variant() { |
1429 | | fn assert_variant(uuid: Uuid, expected: Variant) { |
1430 | | assert_eq!(uuid.get_variant(), expected); |
1431 | | } |
1432 | | |
1433 | | for uuid in some_uuid_v_iter() { |
1434 | | assert_variant(uuid, Variant::RFC4122); |
1435 | | } |
1436 | | |
1437 | | assert_variant( |
1438 | | Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap(), |
1439 | | Variant::Microsoft, |
1440 | | ); |
1441 | | assert_variant( |
1442 | | Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap(), |
1443 | | Variant::Microsoft, |
1444 | | ); |
1445 | | assert_variant( |
1446 | | Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap(), |
1447 | | Variant::NCS, |
1448 | | ); |
1449 | | } |
1450 | | |
1451 | | #[test] |
1452 | | #[cfg_attr( |
1453 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1454 | | wasm_bindgen_test |
1455 | | )] |
1456 | | fn test_from_fields() { |
1457 | | let d1: u32 = 0xa1a2a3a4; |
1458 | | let d2: u16 = 0xb1b2; |
1459 | | let d3: u16 = 0xc1c2; |
1460 | | let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8]; |
1461 | | |
1462 | | let u = Uuid::from_fields(d1, d2, d3, &d4); |
1463 | | |
1464 | | let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; |
1465 | | let result = u.simple().to_string(); |
1466 | | assert_eq!(result, expected); |
1467 | | } |
1468 | | |
1469 | | #[test] |
1470 | | #[cfg_attr( |
1471 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1472 | | wasm_bindgen_test |
1473 | | )] |
1474 | | fn test_from_fields_le() { |
1475 | | let d1: u32 = 0xa4a3a2a1; |
1476 | | let d2: u16 = 0xb2b1; |
1477 | | let d3: u16 = 0xc2c1; |
1478 | | let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8]; |
1479 | | |
1480 | | let u = Uuid::from_fields_le(d1, d2, d3, &d4); |
1481 | | |
1482 | | let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; |
1483 | | let result = u.simple().to_string(); |
1484 | | assert_eq!(result, expected); |
1485 | | } |
1486 | | |
1487 | | #[test] |
1488 | | #[cfg_attr( |
1489 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1490 | | wasm_bindgen_test |
1491 | | )] |
1492 | | fn test_fields_roundtrip() { |
1493 | | let d1_in: u32 = 0xa1a2a3a4; |
1494 | | let d2_in: u16 = 0xb1b2; |
1495 | | let d3_in: u16 = 0xc1c2; |
1496 | | let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8]; |
1497 | | |
1498 | | let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in); |
1499 | | let (d1_out, d2_out, d3_out, d4_out) = u.as_fields(); |
1500 | | |
1501 | | assert_eq!(d1_in, d1_out); |
1502 | | assert_eq!(d2_in, d2_out); |
1503 | | assert_eq!(d3_in, d3_out); |
1504 | | assert_eq!(d4_in, d4_out); |
1505 | | } |
1506 | | |
1507 | | #[test] |
1508 | | #[cfg_attr( |
1509 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1510 | | wasm_bindgen_test |
1511 | | )] |
1512 | | fn test_fields_le_roundtrip() { |
1513 | | let d1_in: u32 = 0xa4a3a2a1; |
1514 | | let d2_in: u16 = 0xb2b1; |
1515 | | let d3_in: u16 = 0xc2c1; |
1516 | | let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8]; |
1517 | | |
1518 | | let u = Uuid::from_fields_le(d1_in, d2_in, d3_in, d4_in); |
1519 | | let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le(); |
1520 | | |
1521 | | assert_eq!(d1_in, d1_out); |
1522 | | assert_eq!(d2_in, d2_out); |
1523 | | assert_eq!(d3_in, d3_out); |
1524 | | assert_eq!(d4_in, d4_out); |
1525 | | } |
1526 | | |
1527 | | #[test] |
1528 | | #[cfg_attr( |
1529 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1530 | | wasm_bindgen_test |
1531 | | )] |
1532 | | fn test_fields_le_are_actually_le() { |
1533 | | let d1_in: u32 = 0xa1a2a3a4; |
1534 | | let d2_in: u16 = 0xb1b2; |
1535 | | let d3_in: u16 = 0xc1c2; |
1536 | | let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8]; |
1537 | | |
1538 | | let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in); |
1539 | | let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le(); |
1540 | | |
1541 | | assert_eq!(d1_in, d1_out.swap_bytes()); |
1542 | | assert_eq!(d2_in, d2_out.swap_bytes()); |
1543 | | assert_eq!(d3_in, d3_out.swap_bytes()); |
1544 | | assert_eq!(d4_in, d4_out); |
1545 | | } |
1546 | | |
1547 | | #[test] |
1548 | | #[cfg_attr( |
1549 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1550 | | wasm_bindgen_test |
1551 | | )] |
1552 | | fn test_u128_roundtrip() { |
1553 | | let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8; |
1554 | | |
1555 | | let u = Uuid::from_u128(v_in); |
1556 | | let v_out = u.as_u128(); |
1557 | | |
1558 | | assert_eq!(v_in, v_out); |
1559 | | } |
1560 | | |
1561 | | #[test] |
1562 | | #[cfg_attr( |
1563 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1564 | | wasm_bindgen_test |
1565 | | )] |
1566 | | fn test_u128_le_roundtrip() { |
1567 | | let v_in: u128 = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1; |
1568 | | |
1569 | | let u = Uuid::from_u128_le(v_in); |
1570 | | let v_out = u.to_u128_le(); |
1571 | | |
1572 | | assert_eq!(v_in, v_out); |
1573 | | } |
1574 | | |
1575 | | #[test] |
1576 | | #[cfg_attr( |
1577 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1578 | | wasm_bindgen_test |
1579 | | )] |
1580 | | fn test_u128_le_is_actually_le() { |
1581 | | let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8; |
1582 | | |
1583 | | let u = Uuid::from_u128(v_in); |
1584 | | let v_out = u.to_u128_le(); |
1585 | | |
1586 | | assert_eq!(v_in, v_out.swap_bytes()); |
1587 | | } |
1588 | | |
1589 | | #[test] |
1590 | | #[cfg_attr( |
1591 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1592 | | wasm_bindgen_test |
1593 | | )] |
1594 | | fn test_u64_pair_roundtrip() { |
1595 | | let high_in: u64 = 0xa1a2a3a4b1b2c1c2; |
1596 | | let low_in: u64 = 0xd1d2d3d4d5d6d7d8; |
1597 | | |
1598 | | let u = Uuid::from_u64_pair(high_in, low_in); |
1599 | | let (high_out, low_out) = u.as_u64_pair(); |
1600 | | |
1601 | | assert_eq!(high_in, high_out); |
1602 | | assert_eq!(low_in, low_out); |
1603 | | } |
1604 | | |
1605 | | #[test] |
1606 | | #[cfg_attr( |
1607 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1608 | | wasm_bindgen_test |
1609 | | )] |
1610 | | fn test_from_slice() { |
1611 | | let b = [ |
1612 | | 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, |
1613 | | 0xd7, 0xd8, |
1614 | | ]; |
1615 | | |
1616 | | let u = Uuid::from_slice(&b).unwrap(); |
1617 | | let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; |
1618 | | |
1619 | | assert_eq!(u.simple().to_string(), expected); |
1620 | | } |
1621 | | |
1622 | | #[test] |
1623 | | #[cfg_attr( |
1624 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1625 | | wasm_bindgen_test |
1626 | | )] |
1627 | | fn test_from_bytes() { |
1628 | | let b = [ |
1629 | | 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, |
1630 | | 0xd7, 0xd8, |
1631 | | ]; |
1632 | | |
1633 | | let u = Uuid::from_bytes(b); |
1634 | | let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"; |
1635 | | |
1636 | | assert_eq!(u.simple().to_string(), expected); |
1637 | | } |
1638 | | |
1639 | | #[test] |
1640 | | #[cfg_attr( |
1641 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1642 | | wasm_bindgen_test |
1643 | | )] |
1644 | | fn test_as_bytes() { |
1645 | | for uuid in some_uuid_v_iter() { |
1646 | | let ub = uuid.as_bytes(); |
1647 | | let ur: &[u8] = uuid.as_ref(); |
1648 | | |
1649 | | assert_eq!(ub.len(), 16); |
1650 | | assert_eq!(ur.len(), 16); |
1651 | | assert!(!ub.iter().all(|&b| b == 0)); |
1652 | | assert!(!ur.iter().all(|&b| b == 0)); |
1653 | | } |
1654 | | } |
1655 | | |
1656 | | #[test] |
1657 | | #[cfg(feature = "std")] |
1658 | | #[cfg_attr( |
1659 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1660 | | wasm_bindgen_test |
1661 | | )] |
1662 | | fn test_convert_vec() { |
1663 | | for uuid in some_uuid_iter() { |
1664 | | let ub: &[u8] = uuid.as_ref(); |
1665 | | |
1666 | | let v: std::vec::Vec<u8> = uuid.into(); |
1667 | | |
1668 | | assert_eq!(&v, ub); |
1669 | | |
1670 | | let uv: Uuid = v.try_into().unwrap(); |
1671 | | |
1672 | | assert_eq!(uv, uuid); |
1673 | | } |
1674 | | } |
1675 | | |
1676 | | #[test] |
1677 | | #[cfg_attr( |
1678 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1679 | | wasm_bindgen_test |
1680 | | )] |
1681 | | fn test_bytes_roundtrip() { |
1682 | | let b_in: crate::Bytes = [ |
1683 | | 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, |
1684 | | 0xd7, 0xd8, |
1685 | | ]; |
1686 | | |
1687 | | let u = Uuid::from_slice(&b_in).unwrap(); |
1688 | | |
1689 | | let b_out = u.as_bytes(); |
1690 | | |
1691 | | assert_eq!(&b_in, b_out); |
1692 | | } |
1693 | | |
1694 | | #[test] |
1695 | | #[cfg_attr( |
1696 | | all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), |
1697 | | wasm_bindgen_test |
1698 | | )] |
1699 | | fn test_bytes_le_roundtrip() { |
1700 | | let b = [ |
1701 | | 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, |
1702 | | 0xd7, 0xd8, |
1703 | | ]; |
1704 | | |
1705 | | let u1 = Uuid::from_bytes(b); |
1706 | | |
1707 | | let b_le = u1.to_bytes_le(); |
1708 | | |
1709 | | let u2 = Uuid::from_bytes_le(b_le); |
1710 | | |
1711 | | assert_eq!(u1, u2); |
1712 | | } |
1713 | | } |