#![no_std]
#![deny(missing_debug_implementations, missing_docs)]
#![allow(clippy::mixed_attributes_style)]
#![doc(
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/uuid/1.23.1"
)]
#[cfg(any(feature = "std", test))]
#[macro_use]
extern crate std;
#[cfg(all(not(feature = "std"), not(test)))]
#[macro_use]
extern crate core as std;
#[macro_use]
mod macros;
mod builder;
mod error;
mod non_nil;
mod parser;
pub mod fmt;
pub mod timestamp;
use core::hash::{Hash, Hasher};
pub use timestamp::{context::NoContext, ClockSequence, Timestamp};
#[cfg(any(feature = "v1", feature = "v6"))]
#[allow(deprecated)]
pub use timestamp::context::Context;
#[cfg(any(feature = "v1", feature = "v6"))]
pub use timestamp::context::ContextV1;
#[cfg(feature = "v7")]
pub use timestamp::context::ContextV7;
#[cfg(feature = "v1")]
#[doc(hidden)]
pub mod v1;
#[cfg(feature = "v3")]
mod v3;
#[cfg(feature = "v4")]
mod v4;
#[cfg(feature = "v5")]
mod v5;
#[cfg(feature = "v6")]
mod v6;
#[cfg(feature = "v7")]
mod v7;
#[cfg(feature = "v8")]
mod v8;
#[cfg(feature = "md5")]
mod md5;
#[cfg(feature = "rng")]
mod rng;
#[cfg(feature = "sha1")]
mod sha1;
mod external;
#[doc(hidden)]
pub mod __macro_support {
pub use crate::std::result::Result::{Err, Ok};
}
pub use crate::{builder::Builder, error::Error, non_nil::NonNilUuid};
pub type Bytes = [u8; 16];
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[repr(u8)]
pub enum Version {
Nil = 0u8,
Mac = 1,
Dce = 2,
Md5 = 3,
Random = 4,
Sha1 = 5,
SortMac = 6,
SortRand = 7,
Custom = 8,
Max = 0x0f,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[repr(u8)]
pub enum Variant {
NCS = 0u8,
RFC4122,
Microsoft,
Future,
}
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
#[cfg_attr(
feature = "borsh",
derive(borsh_derive::BorshDeserialize, borsh_derive::BorshSerialize)
)]
#[cfg_attr(
feature = "bytemuck",
derive(bytemuck::Zeroable, bytemuck::Pod, bytemuck::TransparentWrapper)
)]
#[cfg_attr(
all(uuid_unstable, feature = "zerocopy"),
derive(
zerocopy::IntoBytes,
zerocopy::FromBytes,
zerocopy::KnownLayout,
zerocopy::Immutable,
zerocopy::Unaligned
)
)]
pub struct Uuid(Bytes);
impl Uuid {
pub const NAMESPACE_DNS: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
pub const NAMESPACE_OID: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
pub const NAMESPACE_URL: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
pub const NAMESPACE_X500: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
pub const fn get_variant(&self) -> Variant {
match self.as_bytes()[8] {
x if x & 0x80 == 0x00 => Variant::NCS,
x if x & 0xc0 == 0x80 => Variant::RFC4122,
x if x & 0xe0 == 0xc0 => Variant::Microsoft,
x if x & 0xe0 == 0xe0 => Variant::Future,
_ => Variant::Future,
}
}
pub const fn get_version_num(&self) -> usize {
(self.as_bytes()[6] >> 4) as usize
}
pub const fn get_version(&self) -> Option<Version> {
match self.get_version_num() {
0 if self.is_nil() => Some(Version::Nil),
1 => Some(Version::Mac),
2 => Some(Version::Dce),
3 => Some(Version::Md5),
4 => Some(Version::Random),
5 => Some(Version::Sha1),
6 => Some(Version::SortMac),
7 => Some(Version::SortRand),
8 => Some(Version::Custom),
0xf if self.is_max() => Some(Version::Max),
_ => None,
}
}
pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8]) {
let bytes = self.as_bytes();
let d1 = (bytes[0] as u32) << 24
| (bytes[1] as u32) << 16
| (bytes[2] as u32) << 8
| (bytes[3] as u32);
let d2 = (bytes[4] as u16) << 8 | (bytes[5] as u16);
let d3 = (bytes[6] as u16) << 8 | (bytes[7] as u16);
let d4: &[u8; 8] = bytes[8..16].try_into().unwrap();
(d1, d2, d3, d4)
}
pub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8]) {
let d1 = (self.as_bytes()[0] as u32)
| (self.as_bytes()[1] as u32) << 8
| (self.as_bytes()[2] as u32) << 16
| (self.as_bytes()[3] as u32) << 24;
let d2 = (self.as_bytes()[4] as u16) | (self.as_bytes()[5] as u16) << 8;
let d3 = (self.as_bytes()[6] as u16) | (self.as_bytes()[7] as u16) << 8;
let d4: &[u8; 8] = self.as_bytes()[8..16].try_into().unwrap();
(d1, d2, d3, d4)
}
pub const fn as_u128(&self) -> u128 {
u128::from_be_bytes(*self.as_bytes())
}
pub const fn to_u128_le(&self) -> u128 {
u128::from_le_bytes(*self.as_bytes())
}
pub const fn as_u64_pair(&self) -> (u64, u64) {
let value = self.as_u128();
((value >> 64) as u64, value as u64)
}
#[inline]
pub const fn as_bytes(&self) -> &Bytes {
&self.0
}
#[inline]
pub const fn into_bytes(self) -> Bytes {
self.0
}
pub const fn to_bytes_le(&self) -> Bytes {
[
self.0[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4], self.0[7], self.0[6],
self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14],
self.0[15],
]
}
pub const fn is_nil(&self) -> bool {
self.as_u128() == u128::MIN
}
pub const fn is_max(&self) -> bool {
self.as_u128() == u128::MAX
}
pub const fn encode_buffer() -> [u8; fmt::Urn::LENGTH] {
[0; fmt::Urn::LENGTH]
}
pub const fn get_timestamp(&self) -> Option<Timestamp> {
match self.get_version() {
Some(Version::Mac) => {
let (ticks, counter) = timestamp::decode_gregorian_timestamp(self);
Some(Timestamp::from_gregorian_time(ticks, counter))
}
Some(Version::SortMac) => {
let (ticks, counter) = timestamp::decode_sorted_gregorian_timestamp(self);
Some(Timestamp::from_gregorian_time(ticks, counter))
}
Some(Version::SortRand) => {
let millis = timestamp::decode_unix_timestamp_millis(self);
let seconds = millis / 1000;
let nanos = ((millis % 1000) * 1_000_000) as u32;
Some(Timestamp::from_unix_time(seconds, nanos, 0, 0))
}
_ => None,
}
}
pub const fn get_node_id(&self) -> Option<[u8; 6]> {
match self.get_version() {
Some(Version::Mac) | Some(Version::SortMac) => {
let mut node_id = [0; 6];
node_id[0] = self.0[10];
node_id[1] = self.0[11];
node_id[2] = self.0[12];
node_id[3] = self.0[13];
node_id[4] = self.0[14];
node_id[5] = self.0[15];
Some(node_id)
}
_ => None,
}
}
}
impl Hash for Uuid {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(&self.0);
}
}
impl Default for Uuid {
#[inline]
fn default() -> Self {
Uuid::nil()
}
}
impl AsRef<Uuid> for Uuid {
#[inline]
fn as_ref(&self) -> &Uuid {
self
}
}
impl AsRef<[u8]> for Uuid {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[cfg(feature = "std")]
impl From<Uuid> for std::vec::Vec<u8> {
fn from(value: Uuid) -> Self {
value.0.to_vec()
}
}
#[cfg(feature = "std")]
impl TryFrom<std::vec::Vec<u8>> for Uuid {
type Error = Error;
fn try_from(value: std::vec::Vec<u8>) -> Result<Self, Self::Error> {
Uuid::from_slice(&value)
}
}
#[cfg(feature = "serde")]
pub mod serde {
pub use crate::external::serde_support::{braced, compact, hyphenated, simple, urn};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::std::string::{String, ToString};
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
use wasm_bindgen_test::*;
macro_rules! check {
($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
$buf.clear();
write!($buf, $format, $target).unwrap();
assert!($buf.len() == $len);
assert!($buf.chars().all($cond), "{}", $buf);
assert_eq!(Uuid::parse_str(&$buf).unwrap(), $target);
};
}
pub fn some_uuid_nil() -> Uuid {
Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap()
}
pub fn some_uuid_v1() -> Uuid {
Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap()
}
pub fn some_uuid_v3() -> Uuid {
Uuid::parse_str("bcee7a9c-52f1-30c6-a3cc-8c72ba634990").unwrap()
}
pub fn some_uuid_v4() -> Uuid {
Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap()
}
pub fn some_uuid_v4_2() -> Uuid {
Uuid::parse_str("c0dd0820-b35a-4c56-bc7d-0f0b04241adb").unwrap()
}
pub fn some_uuid_v5() -> Uuid {
Uuid::parse_str("b11f79a5-1e6d-57ce-a4b5-ba8531ea03d0").unwrap()
}
pub fn some_uuid_v6() -> Uuid {
Uuid::parse_str("1e74ba22-0616-6934-8000-010203040506").unwrap()
}
pub fn some_uuid_v7() -> Uuid {
Uuid::parse_str("015c837b-9e84-7db5-b059-c75a84585688").unwrap()
}
pub fn some_uuid_v8() -> Uuid {
Uuid::parse_str("0f0e0d0c-0b0a-8908-8706-050403020100").unwrap()
}
pub fn some_uuid_max() -> Uuid {
Uuid::parse_str("ffffffff-ffff-ffff-ffff-ffffffffffff").unwrap()
}
pub fn some_uuid_iter() -> impl Iterator<Item = Uuid> {
[
some_uuid_nil(),
some_uuid_v1(),
some_uuid_v3(),
some_uuid_v4(),
some_uuid_v5(),
some_uuid_v6(),
some_uuid_v7(),
some_uuid_v8(),
some_uuid_max(),
]
.into_iter()
}
pub fn some_uuid_v_iter() -> impl Iterator<Item = Uuid> {
[
some_uuid_v1(),
some_uuid_v3(),
some_uuid_v4(),
some_uuid_v5(),
some_uuid_v6(),
some_uuid_v7(),
some_uuid_v8(),
]
.into_iter()
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
#[cfg(feature = "std")]
fn test_compare() {
use std::{
cmp::Ordering,
hash::{BuildHasher, BuildHasherDefault, DefaultHasher},
};
let a = some_uuid_v4();
let b = some_uuid_v4_2();
let ah = BuildHasherDefault::<DefaultHasher>::default().hash_one(a);
let bh = BuildHasherDefault::<DefaultHasher>::default().hash_one(b);
assert_eq!(a, a);
assert_eq!(b, b);
assert_eq!(Ordering::Equal, a.cmp(&a));
assert_eq!(Ordering::Equal, b.cmp(&b));
assert_ne!(a, b);
assert_ne!(b, a);
assert_ne!(Ordering::Equal, b.cmp(&a));
assert_ne!(Ordering::Equal, a.cmp(&b));
assert_ne!(ah, bh);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_default() {
let default_uuid = Uuid::default();
let nil_uuid = Uuid::nil();
assert_eq!(default_uuid, nil_uuid);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_display() {
use crate::std::fmt::Write;
for uuid in some_uuid_iter() {
let s = uuid.to_string();
let mut buffer = String::new();
assert_eq!(s, uuid.hyphenated().to_string());
check!(buffer, "{}", some_uuid_v4(), 36, |c| c.is_lowercase()
|| c.is_ascii_digit()
|| c == '-');
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_to_simple_string() {
for uuid in some_uuid_iter() {
let s = uuid.simple().to_string();
assert_eq!(s.len(), 32);
assert!(s.chars().all(|c| c.is_ascii_hexdigit()));
assert_eq!(Uuid::parse_str(&s).unwrap(), uuid);
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_hyphenated_string() {
for uuid in some_uuid_iter() {
let s = uuid.hyphenated().to_string();
assert_eq!(36, s.len());
assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
assert_eq!(Uuid::parse_str(&s).unwrap(), uuid);
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_upper_lower_hex() {
use std::fmt::Write;
let mut buf = String::new();
macro_rules! check {
($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
$buf.clear();
write!($buf, $format, $target).unwrap();
assert_eq!($len, buf.len());
assert!($buf.chars().all($cond), "{}", $buf);
};
}
for uuid in some_uuid_iter() {
check!(buf, "{:x}", uuid, 36, |c| c.is_lowercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:X}", uuid, 36, |c| c.is_uppercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:#x}", uuid, 36, |c| c.is_lowercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:#X}", uuid, 36, |c| c.is_uppercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:X}", uuid.hyphenated(), 36, |c| c.is_uppercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:X}", uuid.simple(), 32, |c| c.is_uppercase()
|| c.is_ascii_digit());
check!(buf, "{:#X}", uuid.hyphenated(), 36, |c| c.is_uppercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:#X}", uuid.simple(), 32, |c| c.is_uppercase()
|| c.is_ascii_digit());
check!(buf, "{:x}", uuid.hyphenated(), 36, |c| c.is_lowercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:x}", uuid.simple(), 32, |c| c.is_lowercase()
|| c.is_ascii_digit());
check!(buf, "{:#x}", uuid.hyphenated(), 36, |c| c.is_lowercase()
|| c.is_ascii_digit()
|| c == '-');
check!(buf, "{:#x}", uuid.simple(), 32, |c| c.is_lowercase()
|| c.is_ascii_digit());
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_to_urn_string() {
for uuid in some_uuid_iter() {
let ss = uuid.urn().to_string();
let s = &ss[9..];
assert!(ss.starts_with("urn:uuid:"));
assert_eq!(s.len(), 36);
assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
assert_eq!(Uuid::parse_str(&ss).unwrap(), uuid);
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_nil() {
let nil = Uuid::nil();
let not_nil = some_uuid_v4();
assert!(nil.is_nil());
assert!(!not_nil.is_nil());
assert_eq!(nil.get_version(), Some(Version::Nil));
assert_eq!(nil.get_variant(), Variant::NCS);
assert_eq!(not_nil.get_version(), Some(Version::Random));
assert_eq!(
nil,
Builder::from_bytes([0; 16])
.with_version(Version::Nil)
.into_uuid()
);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_max() {
let max = Uuid::max();
let not_max = some_uuid_v4();
assert!(max.is_max());
assert!(!not_max.is_max());
assert_eq!(max.get_version(), Some(Version::Max));
assert_eq!(max.get_variant(), Variant::Future);
assert_eq!(not_max.get_version(), Some(Version::Random));
assert_eq!(
max,
Builder::from_bytes([0xff; 16])
.with_version(Version::Max)
.into_uuid()
);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_predefined_namespaces() {
assert_eq!(
Uuid::NAMESPACE_DNS.hyphenated().to_string(),
"6ba7b810-9dad-11d1-80b4-00c04fd430c8"
);
assert_eq!(
Uuid::NAMESPACE_URL.hyphenated().to_string(),
"6ba7b811-9dad-11d1-80b4-00c04fd430c8"
);
assert_eq!(
Uuid::NAMESPACE_OID.hyphenated().to_string(),
"6ba7b812-9dad-11d1-80b4-00c04fd430c8"
);
assert_eq!(
Uuid::NAMESPACE_X500.hyphenated().to_string(),
"6ba7b814-9dad-11d1-80b4-00c04fd430c8"
);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_get_timestamp_unsupported_version() {
for uuid in [
some_uuid_nil(),
some_uuid_v3(),
some_uuid_v4(),
some_uuid_v5(),
some_uuid_v8(),
some_uuid_max(),
] {
assert_ne!(Version::Mac, uuid.get_version().unwrap());
assert_ne!(Version::SortMac, uuid.get_version().unwrap());
assert_ne!(Version::SortRand, uuid.get_version().unwrap());
assert!(uuid.get_timestamp().is_none());
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_get_node_id_unsupported_version() {
for uuid in [
some_uuid_nil(),
some_uuid_v4(),
some_uuid_v7(),
some_uuid_v8(),
some_uuid_max(),
] {
assert_ne!(Version::Mac, uuid.get_version().unwrap());
assert_ne!(Version::SortMac, uuid.get_version().unwrap());
assert!(uuid.get_node_id().is_none());
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_get_version() {
fn assert_version(uuid: Uuid, expected: Version) {
assert_eq!(
uuid.get_version().unwrap(),
expected,
"{uuid} version doesn't match {expected:?}"
);
assert_eq!(
uuid.get_version_num(),
expected as usize,
"{uuid} version doesn't match {}",
expected as usize
);
}
assert_version(some_uuid_nil(), Version::Nil);
assert_version(some_uuid_v1(), Version::Mac);
assert_version(some_uuid_v3(), Version::Md5);
assert_version(some_uuid_v4(), Version::Random);
assert_version(some_uuid_v5(), Version::Sha1);
assert_version(some_uuid_v6(), Version::SortMac);
assert_version(some_uuid_v7(), Version::SortRand);
assert_version(some_uuid_v8(), Version::Custom);
assert_version(some_uuid_max(), Version::Max);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_get_version_non_conforming() {
for case in [
Uuid::from_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]),
Uuid::parse_str("00000000-0000-0000-0000-00000000000f").unwrap(),
Uuid::parse_str("ffffffff-ffff-ffff-ffff-fffffffffff0").unwrap(),
] {
assert_eq!(case.get_version(), None);
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_get_variant() {
fn assert_variant(uuid: Uuid, expected: Variant) {
assert_eq!(uuid.get_variant(), expected);
}
for uuid in some_uuid_v_iter() {
assert_variant(uuid, Variant::RFC4122);
}
assert_variant(
Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap(),
Variant::Microsoft,
);
assert_variant(
Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap(),
Variant::Microsoft,
);
assert_variant(
Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap(),
Variant::NCS,
);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_from_fields() {
let d1: u32 = 0xa1a2a3a4;
let d2: u16 = 0xb1b2;
let d3: u16 = 0xc1c2;
let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
let u = Uuid::from_fields(d1, d2, d3, &d4);
let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
let result = u.simple().to_string();
assert_eq!(result, expected);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_from_fields_le() {
let d1: u32 = 0xa4a3a2a1;
let d2: u16 = 0xb2b1;
let d3: u16 = 0xc2c1;
let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
let u = Uuid::from_fields_le(d1, d2, d3, &d4);
let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
let result = u.simple().to_string();
assert_eq!(result, expected);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_fields_roundtrip() {
let d1_in: u32 = 0xa1a2a3a4;
let d2_in: u16 = 0xb1b2;
let d3_in: u16 = 0xc1c2;
let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
let (d1_out, d2_out, d3_out, d4_out) = u.as_fields();
assert_eq!(d1_in, d1_out);
assert_eq!(d2_in, d2_out);
assert_eq!(d3_in, d3_out);
assert_eq!(d4_in, d4_out);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_fields_le_roundtrip() {
let d1_in: u32 = 0xa4a3a2a1;
let d2_in: u16 = 0xb2b1;
let d3_in: u16 = 0xc2c1;
let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
let u = Uuid::from_fields_le(d1_in, d2_in, d3_in, d4_in);
let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
assert_eq!(d1_in, d1_out);
assert_eq!(d2_in, d2_out);
assert_eq!(d3_in, d3_out);
assert_eq!(d4_in, d4_out);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_fields_le_are_actually_le() {
let d1_in: u32 = 0xa1a2a3a4;
let d2_in: u16 = 0xb1b2;
let d3_in: u16 = 0xc1c2;
let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
assert_eq!(d1_in, d1_out.swap_bytes());
assert_eq!(d2_in, d2_out.swap_bytes());
assert_eq!(d3_in, d3_out.swap_bytes());
assert_eq!(d4_in, d4_out);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_u128_roundtrip() {
let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
let u = Uuid::from_u128(v_in);
let v_out = u.as_u128();
assert_eq!(v_in, v_out);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_u128_le_roundtrip() {
let v_in: u128 = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1;
let u = Uuid::from_u128_le(v_in);
let v_out = u.to_u128_le();
assert_eq!(v_in, v_out);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_u128_le_is_actually_le() {
let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
let u = Uuid::from_u128(v_in);
let v_out = u.to_u128_le();
assert_eq!(v_in, v_out.swap_bytes());
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_u64_pair_roundtrip() {
let high_in: u64 = 0xa1a2a3a4b1b2c1c2;
let low_in: u64 = 0xd1d2d3d4d5d6d7d8;
let u = Uuid::from_u64_pair(high_in, low_in);
let (high_out, low_out) = u.as_u64_pair();
assert_eq!(high_in, high_out);
assert_eq!(low_in, low_out);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_from_slice() {
let b = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u = Uuid::from_slice(&b).unwrap();
let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
assert_eq!(u.simple().to_string(), expected);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_from_bytes() {
let b = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u = Uuid::from_bytes(b);
let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
assert_eq!(u.simple().to_string(), expected);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_as_bytes() {
for uuid in some_uuid_v_iter() {
let ub = uuid.as_bytes();
let ur: &[u8] = uuid.as_ref();
assert_eq!(ub.len(), 16);
assert_eq!(ur.len(), 16);
assert!(!ub.iter().all(|&b| b == 0));
assert!(!ur.iter().all(|&b| b == 0));
}
}
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_convert_vec() {
for uuid in some_uuid_iter() {
let ub: &[u8] = uuid.as_ref();
let v: std::vec::Vec<u8> = uuid.into();
assert_eq!(&v, ub);
let uv: Uuid = v.try_into().unwrap();
assert_eq!(uv, uuid);
}
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_bytes_roundtrip() {
let b_in: crate::Bytes = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u = Uuid::from_slice(&b_in).unwrap();
let b_out = u.as_bytes();
assert_eq!(&b_in, b_out);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
wasm_bindgen_test
)]
fn test_bytes_le_roundtrip() {
let b = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u1 = Uuid::from_bytes(b);
let b_le = u1.to_bytes_le();
let u2 = Uuid::from_bytes_le(b_le);
assert_eq!(u1, u2);
}
}