uuid/lib.rs
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.20.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.20.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.20.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.20.0"
215)]
216
217#[cfg(any(feature = "std", test))]
218#[macro_use]
219extern crate std;
220
221#[cfg(all(not(feature = "std"), not(test)))]
222#[macro_use]
223extern crate core as std;
224
225#[macro_use]
226mod macros;
227
228mod builder;
229mod error;
230mod non_nil;
231mod parser;
232
233pub mod fmt;
234pub mod timestamp;
235
236use core::hash::{Hash, Hasher};
237pub use timestamp::{context::NoContext, ClockSequence, Timestamp};
238
239#[cfg(any(feature = "v1", feature = "v6"))]
240pub use timestamp::context::Context;
241
242#[cfg(feature = "v7")]
243pub use timestamp::context::ContextV7;
244
245#[cfg(feature = "v1")]
246#[doc(hidden)]
247// Soft-deprecated (Rust doesn't support deprecating re-exports)
248// Use `Context` from the crate root instead
249pub mod v1;
250#[cfg(feature = "v3")]
251mod v3;
252#[cfg(feature = "v4")]
253mod v4;
254#[cfg(feature = "v5")]
255mod v5;
256#[cfg(feature = "v6")]
257mod v6;
258#[cfg(feature = "v7")]
259mod v7;
260#[cfg(feature = "v8")]
261mod v8;
262
263#[cfg(feature = "md5")]
264mod md5;
265#[cfg(feature = "rng")]
266mod rng;
267#[cfg(feature = "sha1")]
268mod sha1;
269
270mod external;
271
272#[doc(hidden)]
273pub mod __macro_support {
274 pub use crate::std::result::Result::{Err, Ok};
275}
276
277pub use crate::{builder::Builder, error::Error, non_nil::NonNilUuid};
278
279/// A 128-bit (16 byte) buffer containing the UUID.
280///
281/// # ABI
282///
283/// The `Bytes` type is always guaranteed to be have the same ABI as [`Uuid`].
284pub type Bytes = [u8; 16];
285
286/// The version of the UUID, denoting the generating algorithm.
287///
288/// # References
289///
290/// * [Version Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.2)
291#[derive(Clone, Copy, Debug, PartialEq)]
292#[non_exhaustive]
293#[repr(u8)]
294pub enum Version {
295 /// The "nil" (all zeros) UUID.
296 Nil = 0u8,
297 /// Version 1: Timestamp and node ID.
298 Mac = 1,
299 /// Version 2: DCE Security.
300 Dce = 2,
301 /// Version 3: MD5 hash.
302 Md5 = 3,
303 /// Version 4: Random.
304 Random = 4,
305 /// Version 5: SHA-1 hash.
306 Sha1 = 5,
307 /// Version 6: Sortable Timestamp and node ID.
308 SortMac = 6,
309 /// Version 7: Timestamp and random.
310 SortRand = 7,
311 /// Version 8: Custom.
312 Custom = 8,
313 /// The "max" (all ones) UUID.
314 Max = 0xff,
315}
316
317/// The reserved variants of UUIDs.
318///
319/// # References
320///
321/// * [Variant Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.1)
322#[derive(Clone, Copy, Debug, PartialEq)]
323#[non_exhaustive]
324#[repr(u8)]
325pub enum Variant {
326 /// Reserved by the NCS for backward compatibility.
327 NCS = 0u8,
328 /// As described in the RFC 9562 Specification (default).
329 /// (for backward compatibility it is not yet renamed)
330 RFC4122,
331 /// Reserved by Microsoft for backward compatibility.
332 Microsoft,
333 /// Reserved for future expansion.
334 Future,
335}
336
337/// A Universally Unique Identifier (UUID).
338///
339/// # Examples
340///
341/// Parse a UUID given in the simple format and print it as a urn:
342///
343/// ```
344/// # use uuid::Uuid;
345/// # fn main() -> Result<(), uuid::Error> {
346/// let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?;
347///
348/// println!("{}", my_uuid.urn());
349/// # Ok(())
350/// # }
351/// ```
352///
353/// Create a new random (V4) UUID and print it out in hexadecimal form:
354///
355/// ```
356/// // Note that this requires the `v4` feature enabled in the uuid crate.
357/// # use uuid::Uuid;
358/// # fn main() {
359/// # #[cfg(feature = "v4")] {
360/// let my_uuid = Uuid::new_v4();
361///
362/// println!("{}", my_uuid);
363/// # }
364/// # }
365/// ```
366///
367/// # Formatting
368///
369/// A UUID can be formatted in one of a few ways:
370///
371/// * [`simple`](#method.simple): `a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8`.
372/// * [`hyphenated`](#method.hyphenated):
373/// `a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8`.
374/// * [`urn`](#method.urn): `urn:uuid:A1A2A3A4-B1B2-C1C2-D1D2-D3D4D5D6D7D8`.
375/// * [`braced`](#method.braced): `{a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8}`.
376///
377/// The default representation when formatting a UUID with `Display` is
378/// hyphenated:
379///
380/// ```
381/// # use uuid::Uuid;
382/// # fn main() -> Result<(), uuid::Error> {
383/// let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?;
384///
385/// assert_eq!(
386/// "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8",
387/// my_uuid.to_string(),
388/// );
389/// # Ok(())
390/// # }
391/// ```
392///
393/// Other formats can be specified using adapter methods on the UUID:
394///
395/// ```
396/// # use uuid::Uuid;
397/// # fn main() -> Result<(), uuid::Error> {
398/// let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?;
399///
400/// assert_eq!(
401/// "urn:uuid:a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8",
402/// my_uuid.urn().to_string(),
403/// );
404/// # Ok(())
405/// # }
406/// ```
407///
408/// # Endianness
409///
410/// The specification for UUIDs encodes the integer fields that make up the
411/// value in big-endian order. This crate assumes integer inputs are already in
412/// the correct order by default, regardless of the endianness of the
413/// environment. Most methods that accept integers have a `_le` variant (such as
414/// `from_fields_le`) that assumes any integer values will need to have their
415/// bytes flipped, regardless of the endianness of the environment.
416///
417/// Most users won't need to worry about endianness unless they need to operate
418/// on individual fields (such as when converting between Microsoft GUIDs). The
419/// important things to remember are:
420///
421/// - The endianness is in terms of the fields of the UUID, not the environment.
422/// - The endianness is assumed to be big-endian when there's no `_le` suffix
423/// somewhere.
424/// - Byte-flipping in `_le` methods applies to each integer.
425/// - Endianness roundtrips, so if you create a UUID with `from_fields_le`
426/// you'll get the same values back out with `to_fields_le`.
427///
428/// # ABI
429///
430/// The `Uuid` type is always guaranteed to be have the same ABI as [`Bytes`].
431#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
432#[repr(transparent)]
433// NOTE: Also check `NonNilUuid` when ading new derives here
434#[cfg_attr(
435 feature = "borsh",
436 derive(borsh_derive::BorshDeserialize, borsh_derive::BorshSerialize)
437)]
438#[cfg_attr(
439 feature = "bytemuck",
440 derive(bytemuck::Zeroable, bytemuck::Pod, bytemuck::TransparentWrapper)
441)]
442#[cfg_attr(
443 all(uuid_unstable, feature = "zerocopy"),
444 derive(
445 zerocopy::IntoBytes,
446 zerocopy::FromBytes,
447 zerocopy::KnownLayout,
448 zerocopy::Immutable,
449 zerocopy::Unaligned
450 )
451)]
452pub struct Uuid(Bytes);
453
454impl Uuid {
455 /// UUID namespace for Domain Name System (DNS).
456 pub const NAMESPACE_DNS: Self = Uuid([
457 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
458 0xc8,
459 ]);
460
461 /// UUID namespace for ISO Object Identifiers (OIDs).
462 pub const NAMESPACE_OID: Self = Uuid([
463 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
464 0xc8,
465 ]);
466
467 /// UUID namespace for Uniform Resource Locators (URLs).
468 pub const NAMESPACE_URL: Self = Uuid([
469 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
470 0xc8,
471 ]);
472
473 /// UUID namespace for X.500 Distinguished Names (DNs).
474 pub const NAMESPACE_X500: Self = Uuid([
475 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
476 0xc8,
477 ]);
478
479 /// Returns the variant of the UUID structure.
480 ///
481 /// This determines the interpretation of the structure of the UUID.
482 /// This method simply reads the value of the variant byte. It doesn't
483 /// validate the rest of the UUID as conforming to that variant.
484 ///
485 /// # Examples
486 ///
487 /// Basic usage:
488 ///
489 /// ```
490 /// # use uuid::{Uuid, Variant};
491 /// # fn main() -> Result<(), uuid::Error> {
492 /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?;
493 ///
494 /// assert_eq!(Variant::RFC4122, my_uuid.get_variant());
495 /// # Ok(())
496 /// # }
497 /// ```
498 ///
499 /// # References
500 ///
501 /// * [Variant Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.1)
502 pub const fn get_variant(&self) -> Variant {
503 match self.as_bytes()[8] {
504 x if x & 0x80 == 0x00 => Variant::NCS,
505 x if x & 0xc0 == 0x80 => Variant::RFC4122,
506 x if x & 0xe0 == 0xc0 => Variant::Microsoft,
507 x if x & 0xe0 == 0xe0 => Variant::Future,
508 // The above match arms are actually exhaustive
509 // We just return `Future` here because we can't
510 // use `unreachable!()` in a `const fn`
511 _ => Variant::Future,
512 }
513 }
514
515 /// Returns the version number of the UUID.
516 ///
517 /// This represents the algorithm used to generate the value.
518 /// This method is the future-proof alternative to [`Uuid::get_version`].
519 ///
520 /// # Examples
521 ///
522 /// Basic usage:
523 ///
524 /// ```
525 /// # use uuid::Uuid;
526 /// # fn main() -> Result<(), uuid::Error> {
527 /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?;
528 ///
529 /// assert_eq!(3, my_uuid.get_version_num());
530 /// # Ok(())
531 /// # }
532 /// ```
533 ///
534 /// # References
535 ///
536 /// * [Version Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.2)
537 pub const fn get_version_num(&self) -> usize {
538 (self.as_bytes()[6] >> 4) as usize
539 }
540
541 /// Returns the version of the UUID.
542 ///
543 /// This represents the algorithm used to generate the value.
544 /// If the version field doesn't contain a recognized version then `None`
545 /// is returned. If you're trying to read the version for a future extension
546 /// you can also use [`Uuid::get_version_num`] to unconditionally return a
547 /// number. Future extensions may start to return `Some` once they're
548 /// standardized and supported.
549 ///
550 /// # Examples
551 ///
552 /// Basic usage:
553 ///
554 /// ```
555 /// # use uuid::{Uuid, Version};
556 /// # fn main() -> Result<(), uuid::Error> {
557 /// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?;
558 ///
559 /// assert_eq!(Some(Version::Md5), my_uuid.get_version());
560 /// # Ok(())
561 /// # }
562 /// ```
563 ///
564 /// # References
565 ///
566 /// * [Version Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.2)
567 pub const fn get_version(&self) -> Option<Version> {
568 match self.get_version_num() {
569 0 if self.is_nil() => Some(Version::Nil),
570 1 => Some(Version::Mac),
571 2 => Some(Version::Dce),
572 3 => Some(Version::Md5),
573 4 => Some(Version::Random),
574 5 => Some(Version::Sha1),
575 6 => Some(Version::SortMac),
576 7 => Some(Version::SortRand),
577 8 => Some(Version::Custom),
578 0xf => Some(Version::Max),
579 _ => None,
580 }
581 }
582
583 /// Returns the four field values of the UUID.
584 ///
585 /// These values can be passed to the [`Uuid::from_fields`] method to get
586 /// the original `Uuid` back.
587 ///
588 /// * The first field value represents the first group of (eight) hex
589 /// digits, taken as a big-endian `u32` value. For V1 UUIDs, this field
590 /// represents the low 32 bits of the timestamp.
591 /// * The second field value represents the second group of (four) hex
592 /// digits, taken as a big-endian `u16` value. For V1 UUIDs, this field
593 /// represents the middle 16 bits of the timestamp.
594 /// * The third field value represents the third group of (four) hex digits,
595 /// taken as a big-endian `u16` value. The 4 most significant bits give
596 /// the UUID version, and for V1 UUIDs, the last 12 bits represent the
597 /// high 12 bits of the timestamp.
598 /// * The last field value represents the last two groups of four and twelve
599 /// hex digits, taken in order. The first 1-3 bits of this indicate the
600 /// UUID variant, and for V1 UUIDs, the next 13-15 bits indicate the clock
601 /// sequence and the last 48 bits indicate the node ID.
602 ///
603 /// # Examples
604 ///
605 /// ```
606 /// # use uuid::Uuid;
607 /// # fn main() -> Result<(), uuid::Error> {
608 /// let uuid = Uuid::nil();
609 ///
610 /// assert_eq!(uuid.as_fields(), (0, 0, 0, &[0u8; 8]));
611 ///
612 /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?;
613 ///
614 /// assert_eq!(
615 /// uuid.as_fields(),
616 /// (
617 /// 0xa1a2a3a4,
618 /// 0xb1b2,
619 /// 0xc1c2,
620 /// &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8],
621 /// )
622 /// );
623 /// # Ok(())
624 /// # }
625 /// ```
626 pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8]) {
627 let bytes = self.as_bytes();
628
629 let d1 = (bytes[0] as u32) << 24
630 | (bytes[1] as u32) << 16
631 | (bytes[2] as u32) << 8
632 | (bytes[3] as u32);
633
634 let d2 = (bytes[4] as u16) << 8 | (bytes[5] as u16);
635
636 let d3 = (bytes[6] as u16) << 8 | (bytes[7] as u16);
637
638 let d4: &[u8; 8] = bytes[8..16].try_into().unwrap();
639 (d1, d2, d3, d4)
640 }
641
642 /// Returns the four field values of the UUID in little-endian order.
643 ///
644 /// The bytes in the returned integer fields will be converted from
645 /// big-endian order. This is based on the endianness of the UUID,
646 /// rather than the target environment so bytes will be flipped on both
647 /// big and little endian machines.
648 ///
649 /// # Examples
650 ///
651 /// ```
652 /// use uuid::Uuid;
653 ///
654 /// # fn main() -> Result<(), uuid::Error> {
655 /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?;
656 ///
657 /// assert_eq!(
658 /// uuid.to_fields_le(),
659 /// (
660 /// 0xa4a3a2a1,
661 /// 0xb2b1,
662 /// 0xc2c1,
663 /// &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8],
664 /// )
665 /// );
666 /// # Ok(())
667 /// # }
668 /// ```
669 pub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8]) {
670 let d1 = (self.as_bytes()[0] as u32)
671 | (self.as_bytes()[1] as u32) << 8
672 | (self.as_bytes()[2] as u32) << 16
673 | (self.as_bytes()[3] as u32) << 24;
674
675 let d2 = (self.as_bytes()[4] as u16) | (self.as_bytes()[5] as u16) << 8;
676
677 let d3 = (self.as_bytes()[6] as u16) | (self.as_bytes()[7] as u16) << 8;
678
679 let d4: &[u8; 8] = self.as_bytes()[8..16].try_into().unwrap();
680 (d1, d2, d3, d4)
681 }
682
683 /// Returns a 128bit value containing the value.
684 ///
685 /// The bytes in the UUID will be packed directly into a `u128`.
686 ///
687 /// # Examples
688 ///
689 /// ```
690 /// # use uuid::Uuid;
691 /// # fn main() -> Result<(), uuid::Error> {
692 /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?;
693 ///
694 /// assert_eq!(
695 /// uuid.as_u128(),
696 /// 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8,
697 /// );
698 /// # Ok(())
699 /// # }
700 /// ```
701 pub const fn as_u128(&self) -> u128 {
702 u128::from_be_bytes(*self.as_bytes())
703 }
704
705 /// Returns a 128bit little-endian value containing the value.
706 ///
707 /// The bytes in the `u128` will be flipped to convert into big-endian
708 /// order. This is based on the endianness of the UUID, rather than the
709 /// target environment so bytes will be flipped on both big and little
710 /// endian machines.
711 ///
712 /// Note that this will produce a different result than
713 /// [`Uuid::to_fields_le`], because the entire UUID is reversed, rather
714 /// than reversing the individual fields in-place.
715 ///
716 /// # Examples
717 ///
718 /// ```
719 /// # use uuid::Uuid;
720 /// # fn main() -> Result<(), uuid::Error> {
721 /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?;
722 ///
723 /// assert_eq!(
724 /// uuid.to_u128_le(),
725 /// 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1,
726 /// );
727 /// # Ok(())
728 /// # }
729 /// ```
730 pub const fn to_u128_le(&self) -> u128 {
731 u128::from_le_bytes(*self.as_bytes())
732 }
733
734 /// Returns two 64bit values containing the value.
735 ///
736 /// The bytes in the UUID will be split into two `u64`.
737 /// The first u64 represents the 64 most significant bits,
738 /// the second one represents the 64 least significant.
739 ///
740 /// # Examples
741 ///
742 /// ```
743 /// # use uuid::Uuid;
744 /// # fn main() -> Result<(), uuid::Error> {
745 /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?;
746 /// assert_eq!(
747 /// uuid.as_u64_pair(),
748 /// (0xa1a2a3a4b1b2c1c2, 0xd1d2d3d4d5d6d7d8),
749 /// );
750 /// # Ok(())
751 /// # }
752 /// ```
753 pub const fn as_u64_pair(&self) -> (u64, u64) {
754 let value = self.as_u128();
755 ((value >> 64) as u64, value as u64)
756 }
757
758 /// Returns a slice of 16 octets containing the value.
759 ///
760 /// This method borrows the underlying byte value of the UUID.
761 ///
762 /// # Examples
763 ///
764 /// ```
765 /// # use uuid::Uuid;
766 /// let bytes1 = [
767 /// 0xa1, 0xa2, 0xa3, 0xa4,
768 /// 0xb1, 0xb2,
769 /// 0xc1, 0xc2,
770 /// 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
771 /// ];
772 /// let uuid1 = Uuid::from_bytes_ref(&bytes1);
773 ///
774 /// let bytes2 = uuid1.as_bytes();
775 /// let uuid2 = Uuid::from_bytes_ref(bytes2);
776 ///
777 /// assert_eq!(uuid1, uuid2);
778 ///
779 /// assert!(std::ptr::eq(
780 /// uuid2 as *const Uuid as *const u8,
781 /// &bytes1 as *const [u8; 16] as *const u8,
782 /// ));
783 /// ```
784 #[inline]
785 pub const fn as_bytes(&self) -> &Bytes {
786 &self.0
787 }
788
789 /// Consumes self and returns the underlying byte value of the UUID.
790 ///
791 /// # Examples
792 ///
793 /// ```
794 /// # use uuid::Uuid;
795 /// let bytes = [
796 /// 0xa1, 0xa2, 0xa3, 0xa4,
797 /// 0xb1, 0xb2,
798 /// 0xc1, 0xc2,
799 /// 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
800 /// ];
801 /// let uuid = Uuid::from_bytes(bytes);
802 /// assert_eq!(bytes, uuid.into_bytes());
803 /// ```
804 #[inline]
805 pub const fn into_bytes(self) -> Bytes {
806 self.0
807 }
808
809 /// Returns the bytes of the UUID in little-endian order.
810 ///
811 /// The bytes for each field will be flipped to convert into little-endian order.
812 /// This is based on the endianness of the UUID, rather than the target environment
813 /// so bytes will be flipped on both big and little endian machines.
814 ///
815 /// Note that ordering is applied to each _field_, rather than to the bytes as a whole.
816 /// This ordering is compatible with Microsoft's mixed endian GUID format.
817 ///
818 /// # Examples
819 ///
820 /// ```
821 /// use uuid::Uuid;
822 ///
823 /// # fn main() -> Result<(), uuid::Error> {
824 /// let uuid = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8")?;
825 ///
826 /// assert_eq!(
827 /// uuid.to_bytes_le(),
828 /// ([
829 /// 0xa4, 0xa3, 0xa2, 0xa1, 0xb2, 0xb1, 0xc2, 0xc1, 0xd1, 0xd2,
830 /// 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8
831 /// ])
832 /// );
833 /// # Ok(())
834 /// # }
835 /// ```
836 pub const fn to_bytes_le(&self) -> Bytes {
837 [
838 self.0[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4], self.0[7], self.0[6],
839 self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14],
840 self.0[15],
841 ]
842 }
843
844 /// Tests if the UUID is nil (all zeros).
845 pub const fn is_nil(&self) -> bool {
846 self.as_u128() == u128::MIN
847 }
848
849 /// Tests if the UUID is max (all ones).
850 pub const fn is_max(&self) -> bool {
851 self.as_u128() == u128::MAX
852 }
853
854 /// A buffer that can be used for `encode_...` calls, that is
855 /// guaranteed to be long enough for any of the format adapters.
856 ///
857 /// # Examples
858 ///
859 /// ```
860 /// # use uuid::Uuid;
861 /// let uuid = Uuid::nil();
862 ///
863 /// assert_eq!(
864 /// uuid.simple().encode_lower(&mut Uuid::encode_buffer()),
865 /// "00000000000000000000000000000000"
866 /// );
867 ///
868 /// assert_eq!(
869 /// uuid.hyphenated()
870 /// .encode_lower(&mut Uuid::encode_buffer()),
871 /// "00000000-0000-0000-0000-000000000000"
872 /// );
873 ///
874 /// assert_eq!(
875 /// uuid.urn().encode_lower(&mut Uuid::encode_buffer()),
876 /// "urn:uuid:00000000-0000-0000-0000-000000000000"
877 /// );
878 /// ```
879 pub const fn encode_buffer() -> [u8; fmt::Urn::LENGTH] {
880 [0; fmt::Urn::LENGTH]
881 }
882
883 /// If the UUID is the correct version (v1, v6, or v7) this will return
884 /// the timestamp in a version-agnostic [`Timestamp`]. For other versions
885 /// this will return `None`.
886 ///
887 /// # Roundtripping
888 ///
889 /// This method is unlikely to roundtrip a timestamp in a UUID due to the way
890 /// UUIDs encode timestamps. The timestamp returned from this method will be truncated to
891 /// 100ns precision for version 1 and 6 UUIDs, and to millisecond precision for version 7 UUIDs.
892 pub const fn get_timestamp(&self) -> Option<Timestamp> {
893 match self.get_version() {
894 Some(Version::Mac) => {
895 let (ticks, counter) = timestamp::decode_gregorian_timestamp(self);
896
897 Some(Timestamp::from_gregorian(ticks, counter))
898 }
899 Some(Version::SortMac) => {
900 let (ticks, counter) = timestamp::decode_sorted_gregorian_timestamp(self);
901
902 Some(Timestamp::from_gregorian(ticks, counter))
903 }
904 Some(Version::SortRand) => {
905 let millis = timestamp::decode_unix_timestamp_millis(self);
906
907 let seconds = millis / 1000;
908 let nanos = ((millis % 1000) * 1_000_000) as u32;
909
910 Some(Timestamp::from_unix_time(seconds, nanos, 0, 0))
911 }
912 _ => None,
913 }
914 }
915
916 /// If the UUID is the correct version (v1, or v6) this will return the
917 /// node value as a 6-byte array. For other versions this will return `None`.
918 pub const fn get_node_id(&self) -> Option<[u8; 6]> {
919 match self.get_version() {
920 Some(Version::Mac) | Some(Version::SortMac) => {
921 let mut node_id = [0; 6];
922
923 node_id[0] = self.0[10];
924 node_id[1] = self.0[11];
925 node_id[2] = self.0[12];
926 node_id[3] = self.0[13];
927 node_id[4] = self.0[14];
928 node_id[5] = self.0[15];
929
930 Some(node_id)
931 }
932 _ => None,
933 }
934 }
935}
936
937impl Hash for Uuid {
938 fn hash<H: Hasher>(&self, state: &mut H) {
939 state.write(&self.0);
940 }
941}
942
943impl Default for Uuid {
944 #[inline]
945 fn default() -> Self {
946 Uuid::nil()
947 }
948}
949
950impl AsRef<Uuid> for Uuid {
951 #[inline]
952 fn as_ref(&self) -> &Uuid {
953 self
954 }
955}
956
957impl AsRef<[u8]> for Uuid {
958 #[inline]
959 fn as_ref(&self) -> &[u8] {
960 &self.0
961 }
962}
963
964#[cfg(feature = "std")]
965impl From<Uuid> for std::vec::Vec<u8> {
966 fn from(value: Uuid) -> Self {
967 value.0.to_vec()
968 }
969}
970
971#[cfg(feature = "std")]
972impl TryFrom<std::vec::Vec<u8>> for Uuid {
973 type Error = Error;
974
975 fn try_from(value: std::vec::Vec<u8>) -> Result<Self, Self::Error> {
976 Uuid::from_slice(&value)
977 }
978}
979
980#[cfg(feature = "serde")]
981pub mod serde {
982 //! Adapters for alternative `serde` formats.
983 //!
984 //! This module contains adapters you can use with [`#[serde(with)]`](https://serde.rs/field-attrs.html#with)
985 //! to change the way a [`Uuid`](../struct.Uuid.html) is serialized
986 //! and deserialized.
987
988 pub use crate::external::serde_support::{braced, compact, simple, urn};
989}
990
991#[cfg(test)]
992mod tests {
993 use super::*;
994
995 use crate::std::string::{String, ToString};
996
997 #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
998 use wasm_bindgen_test::*;
999
1000 macro_rules! check {
1001 ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
1002 $buf.clear();
1003 write!($buf, $format, $target).unwrap();
1004 assert!($buf.len() == $len);
1005 assert!($buf.chars().all($cond), "{}", $buf);
1006 };
1007 }
1008
1009 pub const fn new() -> Uuid {
1010 Uuid::from_bytes([
1011 0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
1012 0xA1, 0xE4,
1013 ])
1014 }
1015
1016 pub const fn new2() -> Uuid {
1017 Uuid::from_bytes([
1018 0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
1019 0xA1, 0xE4,
1020 ])
1021 }
1022
1023 #[test]
1024 #[cfg_attr(
1025 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1026 wasm_bindgen_test
1027 )]
1028 fn test_uuid_compare() {
1029 let uuid1 = new();
1030 let uuid2 = new2();
1031
1032 assert_eq!(uuid1, uuid1);
1033 assert_eq!(uuid2, uuid2);
1034
1035 assert_ne!(uuid1, uuid2);
1036 assert_ne!(uuid2, uuid1);
1037 }
1038
1039 #[test]
1040 #[cfg_attr(
1041 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1042 wasm_bindgen_test
1043 )]
1044 fn test_uuid_default() {
1045 let default_uuid = Uuid::default();
1046 let nil_uuid = Uuid::nil();
1047
1048 assert_eq!(default_uuid, nil_uuid);
1049 }
1050
1051 #[test]
1052 #[cfg_attr(
1053 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1054 wasm_bindgen_test
1055 )]
1056 fn test_uuid_display() {
1057 use crate::std::fmt::Write;
1058
1059 let uuid = new();
1060 let s = uuid.to_string();
1061 let mut buffer = String::new();
1062
1063 assert_eq!(s, uuid.hyphenated().to_string());
1064
1065 check!(buffer, "{}", uuid, 36, |c| c.is_lowercase()
1066 || c.is_ascii_digit()
1067 || c == '-');
1068 }
1069
1070 #[test]
1071 #[cfg_attr(
1072 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1073 wasm_bindgen_test
1074 )]
1075 fn test_uuid_lowerhex() {
1076 use crate::std::fmt::Write;
1077
1078 let mut buffer = String::new();
1079 let uuid = new();
1080
1081 check!(buffer, "{:x}", uuid, 36, |c| c.is_lowercase()
1082 || c.is_ascii_digit()
1083 || c == '-');
1084 }
1085
1086 // noinspection RsAssertEqual
1087 #[test]
1088 #[cfg_attr(
1089 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1090 wasm_bindgen_test
1091 )]
1092 fn test_uuid_operator_eq() {
1093 let uuid1 = new();
1094 let uuid1_dup = uuid1;
1095 let uuid2 = new2();
1096
1097 assert!(uuid1 == uuid1);
1098 assert!(uuid1 == uuid1_dup);
1099 assert!(uuid1_dup == uuid1);
1100
1101 assert!(uuid1 != uuid2);
1102 assert!(uuid2 != uuid1);
1103 assert!(uuid1_dup != uuid2);
1104 assert!(uuid2 != uuid1_dup);
1105 }
1106
1107 #[test]
1108 #[cfg_attr(
1109 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1110 wasm_bindgen_test
1111 )]
1112 fn test_uuid_to_string() {
1113 use crate::std::fmt::Write;
1114
1115 let uuid = new();
1116 let s = uuid.to_string();
1117 let mut buffer = String::new();
1118
1119 assert_eq!(s.len(), 36);
1120
1121 check!(buffer, "{}", s, 36, |c| c.is_lowercase()
1122 || c.is_ascii_digit()
1123 || c == '-');
1124 }
1125
1126 #[test]
1127 #[cfg_attr(
1128 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1129 wasm_bindgen_test
1130 )]
1131 fn test_non_conforming() {
1132 let from_bytes =
1133 Uuid::from_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]);
1134
1135 assert_eq!(from_bytes.get_version(), None);
1136 }
1137
1138 #[test]
1139 #[cfg_attr(
1140 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1141 wasm_bindgen_test
1142 )]
1143 fn test_nil() {
1144 let nil = Uuid::nil();
1145 let not_nil = new();
1146
1147 assert!(nil.is_nil());
1148 assert!(!not_nil.is_nil());
1149
1150 assert_eq!(nil.get_version(), Some(Version::Nil));
1151 assert_eq!(not_nil.get_version(), Some(Version::Random));
1152
1153 assert_eq!(
1154 nil,
1155 Builder::from_bytes([0; 16])
1156 .with_version(Version::Nil)
1157 .into_uuid()
1158 );
1159 }
1160
1161 #[test]
1162 #[cfg_attr(
1163 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1164 wasm_bindgen_test
1165 )]
1166 fn test_max() {
1167 let max = Uuid::max();
1168 let not_max = new();
1169
1170 assert!(max.is_max());
1171 assert!(!not_max.is_max());
1172
1173 assert_eq!(max.get_version(), Some(Version::Max));
1174 assert_eq!(not_max.get_version(), Some(Version::Random));
1175
1176 assert_eq!(
1177 max,
1178 Builder::from_bytes([0xff; 16])
1179 .with_version(Version::Max)
1180 .into_uuid()
1181 );
1182 }
1183
1184 #[test]
1185 #[cfg_attr(
1186 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1187 wasm_bindgen_test
1188 )]
1189 fn test_predefined_namespaces() {
1190 assert_eq!(
1191 Uuid::NAMESPACE_DNS.hyphenated().to_string(),
1192 "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
1193 );
1194 assert_eq!(
1195 Uuid::NAMESPACE_URL.hyphenated().to_string(),
1196 "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
1197 );
1198 assert_eq!(
1199 Uuid::NAMESPACE_OID.hyphenated().to_string(),
1200 "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
1201 );
1202 assert_eq!(
1203 Uuid::NAMESPACE_X500.hyphenated().to_string(),
1204 "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
1205 );
1206 }
1207
1208 #[cfg(feature = "v3")]
1209 #[test]
1210 #[cfg_attr(
1211 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1212 wasm_bindgen_test
1213 )]
1214 fn test_get_version_v3() {
1215 let uuid = Uuid::new_v3(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
1216
1217 assert_eq!(uuid.get_version().unwrap(), Version::Md5);
1218 assert_eq!(uuid.get_version_num(), 3);
1219 }
1220
1221 #[test]
1222 #[cfg_attr(
1223 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1224 wasm_bindgen_test
1225 )]
1226 fn test_get_timestamp_unsupported_version() {
1227 let uuid = new();
1228
1229 assert_ne!(Version::Mac, uuid.get_version().unwrap());
1230 assert_ne!(Version::SortMac, uuid.get_version().unwrap());
1231 assert_ne!(Version::SortRand, uuid.get_version().unwrap());
1232
1233 assert!(uuid.get_timestamp().is_none());
1234 }
1235
1236 #[test]
1237 #[cfg_attr(
1238 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1239 wasm_bindgen_test
1240 )]
1241 fn test_get_node_id_unsupported_version() {
1242 let uuid = new();
1243
1244 assert_ne!(Version::Mac, uuid.get_version().unwrap());
1245 assert_ne!(Version::SortMac, uuid.get_version().unwrap());
1246
1247 assert!(uuid.get_node_id().is_none());
1248 }
1249
1250 #[test]
1251 #[cfg_attr(
1252 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1253 wasm_bindgen_test
1254 )]
1255 fn test_get_variant() {
1256 let uuid1 = new();
1257 let uuid2 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
1258 let uuid3 = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
1259 let uuid4 = Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap();
1260 let uuid5 = Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap();
1261 let uuid6 = Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap();
1262
1263 assert_eq!(uuid1.get_variant(), Variant::RFC4122);
1264 assert_eq!(uuid2.get_variant(), Variant::RFC4122);
1265 assert_eq!(uuid3.get_variant(), Variant::RFC4122);
1266 assert_eq!(uuid4.get_variant(), Variant::Microsoft);
1267 assert_eq!(uuid5.get_variant(), Variant::Microsoft);
1268 assert_eq!(uuid6.get_variant(), Variant::NCS);
1269 }
1270
1271 #[test]
1272 #[cfg_attr(
1273 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1274 wasm_bindgen_test
1275 )]
1276 fn test_to_simple_string() {
1277 let uuid1 = new();
1278 let s = uuid1.simple().to_string();
1279
1280 assert_eq!(s.len(), 32);
1281 assert!(s.chars().all(|c| c.is_ascii_hexdigit()));
1282 }
1283
1284 #[test]
1285 #[cfg_attr(
1286 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1287 wasm_bindgen_test
1288 )]
1289 fn test_hyphenated_string() {
1290 let uuid1 = new();
1291 let s = uuid1.hyphenated().to_string();
1292
1293 assert_eq!(36, s.len());
1294 assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
1295 }
1296
1297 #[test]
1298 #[cfg_attr(
1299 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1300 wasm_bindgen_test
1301 )]
1302 fn test_upper_lower_hex() {
1303 use std::fmt::Write;
1304
1305 let mut buf = String::new();
1306 let u = new();
1307
1308 macro_rules! check {
1309 ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
1310 $buf.clear();
1311 write!($buf, $format, $target).unwrap();
1312 assert_eq!($len, buf.len());
1313 assert!($buf.chars().all($cond), "{}", $buf);
1314 };
1315 }
1316
1317 check!(buf, "{:x}", u, 36, |c| c.is_lowercase()
1318 || c.is_ascii_digit()
1319 || c == '-');
1320 check!(buf, "{:X}", u, 36, |c| c.is_uppercase()
1321 || c.is_ascii_digit()
1322 || c == '-');
1323 check!(buf, "{:#x}", u, 36, |c| c.is_lowercase()
1324 || c.is_ascii_digit()
1325 || c == '-');
1326 check!(buf, "{:#X}", u, 36, |c| c.is_uppercase()
1327 || c.is_ascii_digit()
1328 || c == '-');
1329
1330 check!(buf, "{:X}", u.hyphenated(), 36, |c| c.is_uppercase()
1331 || c.is_ascii_digit()
1332 || c == '-');
1333 check!(buf, "{:X}", u.simple(), 32, |c| c.is_uppercase()
1334 || c.is_ascii_digit());
1335 check!(buf, "{:#X}", u.hyphenated(), 36, |c| c.is_uppercase()
1336 || c.is_ascii_digit()
1337 || c == '-');
1338 check!(buf, "{:#X}", u.simple(), 32, |c| c.is_uppercase()
1339 || c.is_ascii_digit());
1340
1341 check!(buf, "{:x}", u.hyphenated(), 36, |c| c.is_lowercase()
1342 || c.is_ascii_digit()
1343 || c == '-');
1344 check!(buf, "{:x}", u.simple(), 32, |c| c.is_lowercase()
1345 || c.is_ascii_digit());
1346 check!(buf, "{:#x}", u.hyphenated(), 36, |c| c.is_lowercase()
1347 || c.is_ascii_digit()
1348 || c == '-');
1349 check!(buf, "{:#x}", u.simple(), 32, |c| c.is_lowercase()
1350 || c.is_ascii_digit());
1351 }
1352
1353 #[test]
1354 #[cfg_attr(
1355 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1356 wasm_bindgen_test
1357 )]
1358 fn test_to_urn_string() {
1359 let uuid1 = new();
1360 let ss = uuid1.urn().to_string();
1361 let s = &ss[9..];
1362
1363 assert!(ss.starts_with("urn:uuid:"));
1364 assert_eq!(s.len(), 36);
1365 assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
1366 }
1367
1368 #[test]
1369 #[cfg_attr(
1370 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1371 wasm_bindgen_test
1372 )]
1373 fn test_to_simple_string_matching() {
1374 let uuid1 = new();
1375
1376 let hs = uuid1.hyphenated().to_string();
1377 let ss = uuid1.simple().to_string();
1378
1379 let hsn = hs.chars().filter(|&c| c != '-').collect::<String>();
1380
1381 assert_eq!(hsn, ss);
1382 }
1383
1384 #[test]
1385 #[cfg_attr(
1386 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1387 wasm_bindgen_test
1388 )]
1389 fn test_string_roundtrip() {
1390 let uuid = new();
1391
1392 let hs = uuid.hyphenated().to_string();
1393 let uuid_hs = Uuid::parse_str(&hs).unwrap();
1394 assert_eq!(uuid_hs, uuid);
1395
1396 let ss = uuid.to_string();
1397 let uuid_ss = Uuid::parse_str(&ss).unwrap();
1398 assert_eq!(uuid_ss, uuid);
1399 }
1400
1401 #[test]
1402 #[cfg_attr(
1403 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1404 wasm_bindgen_test
1405 )]
1406 fn test_from_fields() {
1407 let d1: u32 = 0xa1a2a3a4;
1408 let d2: u16 = 0xb1b2;
1409 let d3: u16 = 0xc1c2;
1410 let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1411
1412 let u = Uuid::from_fields(d1, d2, d3, &d4);
1413
1414 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1415 let result = u.simple().to_string();
1416 assert_eq!(result, expected);
1417 }
1418
1419 #[test]
1420 #[cfg_attr(
1421 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1422 wasm_bindgen_test
1423 )]
1424 fn test_from_fields_le() {
1425 let d1: u32 = 0xa4a3a2a1;
1426 let d2: u16 = 0xb2b1;
1427 let d3: u16 = 0xc2c1;
1428 let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1429
1430 let u = Uuid::from_fields_le(d1, d2, d3, &d4);
1431
1432 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1433 let result = u.simple().to_string();
1434 assert_eq!(result, expected);
1435 }
1436
1437 #[test]
1438 #[cfg_attr(
1439 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1440 wasm_bindgen_test
1441 )]
1442 fn test_as_fields() {
1443 let u = new();
1444 let (d1, d2, d3, d4) = u.as_fields();
1445
1446 assert_ne!(d1, 0);
1447 assert_ne!(d2, 0);
1448 assert_ne!(d3, 0);
1449 assert_eq!(d4.len(), 8);
1450 assert!(!d4.iter().all(|&b| b == 0));
1451 }
1452
1453 #[test]
1454 #[cfg_attr(
1455 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1456 wasm_bindgen_test
1457 )]
1458 fn test_fields_roundtrip() {
1459 let d1_in: u32 = 0xa1a2a3a4;
1460 let d2_in: u16 = 0xb1b2;
1461 let d3_in: u16 = 0xc1c2;
1462 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1463
1464 let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
1465 let (d1_out, d2_out, d3_out, d4_out) = u.as_fields();
1466
1467 assert_eq!(d1_in, d1_out);
1468 assert_eq!(d2_in, d2_out);
1469 assert_eq!(d3_in, d3_out);
1470 assert_eq!(d4_in, d4_out);
1471 }
1472
1473 #[test]
1474 #[cfg_attr(
1475 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1476 wasm_bindgen_test
1477 )]
1478 fn test_fields_le_roundtrip() {
1479 let d1_in: u32 = 0xa4a3a2a1;
1480 let d2_in: u16 = 0xb2b1;
1481 let d3_in: u16 = 0xc2c1;
1482 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1483
1484 let u = Uuid::from_fields_le(d1_in, d2_in, d3_in, d4_in);
1485 let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
1486
1487 assert_eq!(d1_in, d1_out);
1488 assert_eq!(d2_in, d2_out);
1489 assert_eq!(d3_in, d3_out);
1490 assert_eq!(d4_in, d4_out);
1491 }
1492
1493 #[test]
1494 #[cfg_attr(
1495 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1496 wasm_bindgen_test
1497 )]
1498 fn test_fields_le_are_actually_le() {
1499 let d1_in: u32 = 0xa1a2a3a4;
1500 let d2_in: u16 = 0xb1b2;
1501 let d3_in: u16 = 0xc1c2;
1502 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1503
1504 let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
1505 let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
1506
1507 assert_eq!(d1_in, d1_out.swap_bytes());
1508 assert_eq!(d2_in, d2_out.swap_bytes());
1509 assert_eq!(d3_in, d3_out.swap_bytes());
1510 assert_eq!(d4_in, d4_out);
1511 }
1512
1513 #[test]
1514 #[cfg_attr(
1515 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1516 wasm_bindgen_test
1517 )]
1518 fn test_from_u128() {
1519 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1520
1521 let u = Uuid::from_u128(v_in);
1522
1523 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1524 let result = u.simple().to_string();
1525 assert_eq!(result, expected);
1526 }
1527
1528 #[test]
1529 #[cfg_attr(
1530 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1531 wasm_bindgen_test
1532 )]
1533 fn test_from_u128_le() {
1534 let v_in: u128 = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1;
1535
1536 let u = Uuid::from_u128_le(v_in);
1537
1538 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1539 let result = u.simple().to_string();
1540 assert_eq!(result, expected);
1541 }
1542
1543 #[test]
1544 #[cfg_attr(
1545 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1546 wasm_bindgen_test
1547 )]
1548 fn test_from_u64_pair() {
1549 let high_in: u64 = 0xa1a2a3a4b1b2c1c2;
1550 let low_in: u64 = 0xd1d2d3d4d5d6d7d8;
1551
1552 let u = Uuid::from_u64_pair(high_in, low_in);
1553
1554 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1555 let result = u.simple().to_string();
1556 assert_eq!(result, expected);
1557 }
1558
1559 #[test]
1560 #[cfg_attr(
1561 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1562 wasm_bindgen_test
1563 )]
1564 fn test_u128_roundtrip() {
1565 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1566
1567 let u = Uuid::from_u128(v_in);
1568 let v_out = u.as_u128();
1569
1570 assert_eq!(v_in, v_out);
1571 }
1572
1573 #[test]
1574 #[cfg_attr(
1575 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1576 wasm_bindgen_test
1577 )]
1578 fn test_u128_le_roundtrip() {
1579 let v_in: u128 = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1;
1580
1581 let u = Uuid::from_u128_le(v_in);
1582 let v_out = u.to_u128_le();
1583
1584 assert_eq!(v_in, v_out);
1585 }
1586
1587 #[test]
1588 #[cfg_attr(
1589 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1590 wasm_bindgen_test
1591 )]
1592 fn test_u64_pair_roundtrip() {
1593 let high_in: u64 = 0xa1a2a3a4b1b2c1c2;
1594 let low_in: u64 = 0xd1d2d3d4d5d6d7d8;
1595
1596 let u = Uuid::from_u64_pair(high_in, low_in);
1597 let (high_out, low_out) = u.as_u64_pair();
1598
1599 assert_eq!(high_in, high_out);
1600 assert_eq!(low_in, low_out);
1601 }
1602
1603 #[test]
1604 #[cfg_attr(
1605 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1606 wasm_bindgen_test
1607 )]
1608 fn test_u128_le_is_actually_le() {
1609 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1610
1611 let u = Uuid::from_u128(v_in);
1612 let v_out = u.to_u128_le();
1613
1614 assert_eq!(v_in, v_out.swap_bytes());
1615 }
1616
1617 #[test]
1618 #[cfg_attr(
1619 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1620 wasm_bindgen_test
1621 )]
1622 fn test_from_slice() {
1623 let b = [
1624 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1625 0xd7, 0xd8,
1626 ];
1627
1628 let u = Uuid::from_slice(&b).unwrap();
1629 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1630
1631 assert_eq!(u.simple().to_string(), expected);
1632 }
1633
1634 #[test]
1635 #[cfg_attr(
1636 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1637 wasm_bindgen_test
1638 )]
1639 fn test_from_bytes() {
1640 let b = [
1641 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1642 0xd7, 0xd8,
1643 ];
1644
1645 let u = Uuid::from_bytes(b);
1646 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1647
1648 assert_eq!(u.simple().to_string(), expected);
1649 }
1650
1651 #[test]
1652 #[cfg_attr(
1653 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1654 wasm_bindgen_test
1655 )]
1656 fn test_as_bytes() {
1657 let u = new();
1658 let ub = u.as_bytes();
1659 let ur: &[u8] = u.as_ref();
1660
1661 assert_eq!(ub.len(), 16);
1662 assert_eq!(ur.len(), 16);
1663 assert!(!ub.iter().all(|&b| b == 0));
1664 assert!(!ur.iter().all(|&b| b == 0));
1665 }
1666
1667 #[test]
1668 #[cfg(feature = "std")]
1669 #[cfg_attr(
1670 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1671 wasm_bindgen_test
1672 )]
1673 fn test_convert_vec() {
1674 let u = new();
1675 let ub: &[u8] = u.as_ref();
1676
1677 let v: std::vec::Vec<u8> = u.into();
1678
1679 assert_eq!(&v, ub);
1680
1681 let uv: Uuid = v.try_into().unwrap();
1682
1683 assert_eq!(uv, u);
1684 }
1685
1686 #[test]
1687 #[cfg_attr(
1688 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1689 wasm_bindgen_test
1690 )]
1691 fn test_bytes_roundtrip() {
1692 let b_in: crate::Bytes = [
1693 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1694 0xd7, 0xd8,
1695 ];
1696
1697 let u = Uuid::from_slice(&b_in).unwrap();
1698
1699 let b_out = u.as_bytes();
1700
1701 assert_eq!(&b_in, b_out);
1702 }
1703
1704 #[test]
1705 #[cfg_attr(
1706 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1707 wasm_bindgen_test
1708 )]
1709 fn test_bytes_le_roundtrip() {
1710 let b = [
1711 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1712 0xd7, 0xd8,
1713 ];
1714
1715 let u1 = Uuid::from_bytes(b);
1716
1717 let b_le = u1.to_bytes_le();
1718
1719 let u2 = Uuid::from_bytes_le(b_le);
1720
1721 assert_eq!(u1, u2);
1722 }
1723
1724 #[test]
1725 #[cfg_attr(
1726 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1727 wasm_bindgen_test
1728 )]
1729 fn test_iterbytes_impl_for_uuid() {
1730 let mut set = std::collections::HashSet::new();
1731 let id1 = new();
1732 let id2 = new2();
1733 set.insert(id1);
1734
1735 assert!(set.contains(&id1));
1736 assert!(!set.contains(&id2));
1737 }
1738}