1#![cfg_attr(docsrs, feature(doc_cfg))]
209#![no_std]
210#![deny(missing_debug_implementations, missing_docs)]
211#![allow(clippy::mixed_attributes_style)]
212#![doc(
213 html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
214 html_favicon_url = "https://www.rust-lang.org/favicon.ico",
215 html_root_url = "https://docs.rs/uuid/1.26.1"
216)]
217
218#[cfg(any(feature = "std", test))]
219#[macro_use]
220extern crate std;
221
222#[cfg(all(not(feature = "std"), not(test)))]
223#[macro_use]
224extern crate core as std;
225
226#[macro_use]
227mod macros;
228
229mod builder;
230mod error;
231mod non_nil;
232mod parser;
233
234pub mod fmt;
235pub mod timestamp;
236
237use core::hash::{Hash, Hasher};
238pub use timestamp::{context::NoContext, ClockSequence, Timestamp};
239
240#[cfg(any(feature = "v1", feature = "v6"))]
241#[allow(deprecated)]
242pub use timestamp::context::Context;
243
244#[cfg(any(feature = "v1", feature = "v6"))]
245pub use timestamp::context::ContextV1;
246
247#[cfg(feature = "v7")]
248pub use timestamp::context::ContextV7;
249
250#[cfg(feature = "v1")]
251#[doc(hidden)]
252pub mod v1;
255#[cfg(feature = "v3")]
256mod v3;
257#[cfg(feature = "v4")]
258mod v4;
259#[cfg(feature = "v5")]
260mod v5;
261#[cfg(feature = "v6")]
262mod v6;
263#[cfg(feature = "v7")]
264mod v7;
265#[cfg(feature = "v8")]
266mod v8;
267
268#[cfg(feature = "md5")]
269mod md5;
270#[cfg(feature = "rng")]
271mod rng;
272#[cfg(feature = "sha1")]
273mod sha1;
274
275mod external;
276
277#[doc(hidden)]
278pub mod __macro_support {
279 pub use crate::std::result::Result::{Err, Ok};
280}
281
282pub use crate::{builder::Builder, error::Error, non_nil::NonNilUuid};
283
284pub type Bytes = [u8; 16];
290
291#[derive(#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for Version { }
#[automatically_derived]
impl ::core::clone::Clone for Version {
#[inline]
fn clone(&self) -> Version { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Version { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Version {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Version::Nil => "Nil",
Version::Mac => "Mac",
Version::Dce => "Dce",
Version::Md5 => "Md5",
Version::Random => "Random",
Version::Sha1 => "Sha1",
Version::SortMac => "SortMac",
Version::SortRand => "SortRand",
Version::Custom => "Custom",
Version::Max => "Max",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for Version { }
#[automatically_derived]
impl ::core::cmp::PartialEq for Version {
#[inline]
fn eq(&self, other: &Version) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
297#[non_exhaustive]
298#[repr(u8)]
299pub enum Version {
300 Nil = 0u8,
302 Mac = 1,
304 Dce = 2,
306 Md5 = 3,
308 Random = 4,
310 Sha1 = 5,
312 SortMac = 6,
314 SortRand = 7,
316 Custom = 8,
318 Max = 0x0f,
320}
321
322#[derive(#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for Variant { }
#[automatically_derived]
impl ::core::clone::Clone for Variant {
#[inline]
fn clone(&self) -> Variant { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Variant { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Variant {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Variant::NCS => "NCS",
Variant::RFC4122 => "RFC4122",
Variant::Microsoft => "Microsoft",
Variant::Future => "Future",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for Variant { }
#[automatically_derived]
impl ::core::cmp::PartialEq for Variant {
#[inline]
fn eq(&self, other: &Variant) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
332#[non_exhaustive]
333#[repr(u8)]
334pub enum Variant {
335 NCS = 0u8,
339 RFC4122,
343 Microsoft,
345 Future,
349}
350
351#[derive(#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for Uuid { }
#[automatically_derived]
impl ::core::clone::Clone for Uuid {
#[inline]
fn clone(&self) -> Uuid {
let _: ::core::clone::AssertParamIsClone<Bytes>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Uuid { }Copy, #[automatically_derived]
impl ::core::cmp::Eq for Uuid {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Bytes>;
}
}Eq, #[automatically_derived]
impl ::core::cmp::Ord for Uuid {
#[inline]
fn cmp(&self, other: &Uuid) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for Uuid { }
#[automatically_derived]
impl ::core::cmp::PartialEq for Uuid {
#[inline]
fn eq(&self, other: &Uuid) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::PartialOrd for Uuid {
#[inline]
fn partial_cmp(&self, other: &Uuid)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}PartialOrd)]
446#[repr(transparent)]
447#[cfg_attr(
449 feature = "borsh",
450 derive(borsh_derive::BorshDeserialize, borsh_derive::BorshSerialize)
451)]
452#[cfg_attr(
453 feature = "bytemuck",
454 derive(bytemuck::Zeroable, bytemuck::Pod, bytemuck::TransparentWrapper)
455)]
456#[cfg_attr(
457 all(uuid_unstable, feature = "zerocopy"),
458 derive(
459 zerocopy::IntoBytes,
460 zerocopy::FromBytes,
461 zerocopy::KnownLayout,
462 zerocopy::Immutable,
463 zerocopy::Unaligned
464 )
465)]
466pub struct Uuid(Bytes);
467
468impl Uuid {
469 pub const NAMESPACE_DNS: Self = Uuid([
471 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
472 0xc8,
473 ]);
474
475 pub const NAMESPACE_OID: Self = Uuid([
477 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
478 0xc8,
479 ]);
480
481 pub const NAMESPACE_URL: Self = Uuid([
483 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
484 0xc8,
485 ]);
486
487 pub const NAMESPACE_X500: Self = Uuid([
489 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
490 0xc8,
491 ]);
492
493 pub const fn get_variant(&self) -> Variant {
517 match self.as_bytes()[8] {
518 x if x & 0x80 == 0x00 => Variant::NCS,
519 x if x & 0xc0 == 0x80 => Variant::RFC4122,
520 x if x & 0xe0 == 0xc0 => Variant::Microsoft,
521 x if x & 0xe0 == 0xe0 => Variant::Future,
522 _ => Variant::Future,
526 }
527 }
528
529 pub const fn get_version_num(&self) -> usize {
552 (self.as_bytes()[6] >> 4) as usize
553 }
554
555 pub const fn get_version(&self) -> Option<Version> {
582 match self.get_version_num() {
583 0 if self.is_nil() => Some(Version::Nil),
584 1 => Some(Version::Mac),
585 2 => Some(Version::Dce),
586 3 => Some(Version::Md5),
587 4 => Some(Version::Random),
588 5 => Some(Version::Sha1),
589 6 => Some(Version::SortMac),
590 7 => Some(Version::SortRand),
591 8 => Some(Version::Custom),
592 0xf if self.is_max() => Some(Version::Max),
593 _ => None,
594 }
595 }
596
597 pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8]) {
641 let bytes = self.as_bytes();
642
643 let d1 = (bytes[0] as u32) << 24
644 | (bytes[1] as u32) << 16
645 | (bytes[2] as u32) << 8
646 | (bytes[3] as u32);
647
648 let d2 = (bytes[4] as u16) << 8 | (bytes[5] as u16);
649
650 let d3 = (bytes[6] as u16) << 8 | (bytes[7] as u16);
651
652 let d4: &[u8; 8] = bytes[8..16].try_into().unwrap();
653 (d1, d2, d3, d4)
654 }
655
656 pub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8]) {
684 let d1 = (self.as_bytes()[0] as u32)
685 | (self.as_bytes()[1] as u32) << 8
686 | (self.as_bytes()[2] as u32) << 16
687 | (self.as_bytes()[3] as u32) << 24;
688
689 let d2 = (self.as_bytes()[4] as u16) | (self.as_bytes()[5] as u16) << 8;
690
691 let d3 = (self.as_bytes()[6] as u16) | (self.as_bytes()[7] as u16) << 8;
692
693 let d4: &[u8; 8] = self.as_bytes()[8..16].try_into().unwrap();
694 (d1, d2, d3, d4)
695 }
696
697 pub const fn as_u128(&self) -> u128 {
716 u128::from_be_bytes(*self.as_bytes())
717 }
718
719 pub const fn to_u128_le(&self) -> u128 {
745 u128::from_le_bytes(*self.as_bytes())
746 }
747
748 pub const fn as_u64_pair(&self) -> (u64, u64) {
768 let value = self.as_u128();
769 ((value >> 64) as u64, value as u64)
770 }
771
772 #[inline]
799 pub const fn as_bytes(&self) -> &Bytes {
800 &self.0
801 }
802
803 #[inline]
819 pub const fn into_bytes(self) -> Bytes {
820 self.0
821 }
822
823 pub const fn to_bytes_le(&self) -> Bytes {
851 [
852 self.0[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4], self.0[7], self.0[6],
853 self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14],
854 self.0[15],
855 ]
856 }
857
858 pub const fn is_nil(&self) -> bool {
860 self.as_u128() == u128::MIN
861 }
862
863 pub const fn is_max(&self) -> bool {
865 self.as_u128() == u128::MAX
866 }
867
868 pub const fn encode_buffer() -> [u8; fmt::Urn::LENGTH] {
894 [0; fmt::Urn::LENGTH]
895 }
896
897 pub const fn get_timestamp(&self) -> Option<Timestamp> {
907 match self.get_version() {
908 Some(Version::Mac) => {
909 let (ticks, counter) = timestamp::decode_gregorian_timestamp(self);
910
911 Some(Timestamp::from_gregorian_time(ticks, counter))
912 }
913 Some(Version::SortMac) => {
914 let (ticks, counter) = timestamp::decode_sorted_gregorian_timestamp(self);
915
916 Some(Timestamp::from_gregorian_time(ticks, counter))
917 }
918 Some(Version::SortRand) => {
919 let millis = timestamp::decode_unix_timestamp_millis(self);
920
921 let seconds = millis / 1000;
922 let nanos = ((millis % 1000) * 1_000_000) as u32;
923
924 Some(Timestamp::from_unix_time(seconds, nanos, 0, 0))
925 }
926 _ => None,
927 }
928 }
929
930 pub const fn get_node_id(&self) -> Option<[u8; 6]> {
933 match self.get_version() {
934 Some(Version::Mac) | Some(Version::SortMac) => {
935 let mut node_id = [0; 6];
936
937 node_id[0] = self.0[10];
938 node_id[1] = self.0[11];
939 node_id[2] = self.0[12];
940 node_id[3] = self.0[13];
941 node_id[4] = self.0[14];
942 node_id[5] = self.0[15];
943
944 Some(node_id)
945 }
946 _ => None,
947 }
948 }
949}
950
951impl Hash for Uuid {
952 fn hash<H: Hasher>(&self, state: &mut H) {
953 state.write(&self.0);
954 }
955}
956
957impl Default for Uuid {
958 #[inline]
959 fn default() -> Self {
960 Uuid::nil()
961 }
962}
963
964impl AsRef<Uuid> for Uuid {
965 #[inline]
966 fn as_ref(&self) -> &Uuid {
967 self
968 }
969}
970
971impl AsRef<[u8]> for Uuid {
972 #[inline]
973 fn as_ref(&self) -> &[u8] {
974 &self.0
975 }
976}
977
978#[cfg(feature = "std")]
979impl From<Uuid> for std::vec::Vec<u8> {
980 fn from(value: Uuid) -> Self {
981 value.0.to_vec()
982 }
983}
984
985#[cfg(feature = "std")]
986impl TryFrom<std::vec::Vec<u8>> for Uuid {
987 type Error = Error;
988
989 fn try_from(value: std::vec::Vec<u8>) -> Result<Self, Self::Error> {
990 Uuid::from_slice(&value)
991 }
992}
993
994#[cfg(feature = "serde")]
995pub mod serde {
996 pub use crate::external::serde_support::{braced, bytes, compact, hyphenated, simple, urn};
1003}
1004
1005#[cfg(test)]
1006mod tests {
1007 use super::*;
1008
1009 use crate::std::string::{String, ToString};
1010
1011 #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
1012 use wasm_bindgen_test::*;
1013
1014 macro_rules! check {
1015 ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
1016 $buf.clear();
1017 write!($buf, $format, $target).unwrap();
1018 assert!($buf.len() == $len);
1019 assert!($buf.chars().all($cond), "{}", $buf);
1020
1021 assert_eq!(Uuid::parse_str(&$buf).unwrap(), $target);
1022 };
1023 }
1024
1025 pub fn some_uuid_nil() -> Uuid {
1026 Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap()
1027 }
1028
1029 pub fn some_uuid_v1() -> Uuid {
1030 Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap()
1031 }
1032
1033 pub fn some_uuid_v3() -> Uuid {
1034 Uuid::parse_str("bcee7a9c-52f1-30c6-a3cc-8c72ba634990").unwrap()
1035 }
1036
1037 pub fn some_uuid_v4() -> Uuid {
1038 Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap()
1039 }
1040
1041 pub fn some_uuid_v4_2() -> Uuid {
1042 Uuid::parse_str("c0dd0820-b35a-4c56-bc7d-0f0b04241adb").unwrap()
1043 }
1044
1045 pub fn some_uuid_v5() -> Uuid {
1046 Uuid::parse_str("b11f79a5-1e6d-57ce-a4b5-ba8531ea03d0").unwrap()
1047 }
1048
1049 pub fn some_uuid_v6() -> Uuid {
1050 Uuid::parse_str("1e74ba22-0616-6934-8000-010203040506").unwrap()
1051 }
1052
1053 pub fn some_uuid_v7() -> Uuid {
1054 Uuid::parse_str("015c837b-9e84-7db5-b059-c75a84585688").unwrap()
1055 }
1056
1057 pub fn some_uuid_v8() -> Uuid {
1058 Uuid::parse_str("0f0e0d0c-0b0a-8908-8706-050403020100").unwrap()
1059 }
1060
1061 pub fn some_uuid_max() -> Uuid {
1062 Uuid::parse_str("ffffffff-ffff-ffff-ffff-ffffffffffff").unwrap()
1063 }
1064
1065 pub fn some_uuid_iter() -> impl Iterator<Item = Uuid> {
1066 [
1067 some_uuid_nil(),
1068 some_uuid_v1(),
1069 some_uuid_v3(),
1070 some_uuid_v4(),
1071 some_uuid_v5(),
1072 some_uuid_v6(),
1073 some_uuid_v7(),
1074 some_uuid_v8(),
1075 some_uuid_max(),
1076 ]
1077 .into_iter()
1078 }
1079
1080 pub fn some_uuid_v_iter() -> impl Iterator<Item = Uuid> {
1081 [
1082 some_uuid_v1(),
1083 some_uuid_v3(),
1084 some_uuid_v4(),
1085 some_uuid_v5(),
1086 some_uuid_v6(),
1087 some_uuid_v7(),
1088 some_uuid_v8(),
1089 ]
1090 .into_iter()
1091 }
1092
1093 #[test]
1094 #[cfg_attr(
1095 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1096 wasm_bindgen_test
1097 )]
1098 #[cfg(feature = "std")]
1099 fn test_compare() {
1100 use std::{
1101 cmp::Ordering,
1102 hash::{BuildHasher, BuildHasherDefault, DefaultHasher},
1103 };
1104
1105 let a = some_uuid_v4();
1106 let b = some_uuid_v4_2();
1107
1108 let ah = BuildHasherDefault::<DefaultHasher>::default().hash_one(a);
1109 let bh = BuildHasherDefault::<DefaultHasher>::default().hash_one(b);
1110
1111 assert_eq!(a, a);
1112 assert_eq!(b, b);
1113 assert_eq!(Ordering::Equal, a.cmp(&a));
1114 assert_eq!(Ordering::Equal, b.cmp(&b));
1115
1116 assert_ne!(a, b);
1117 assert_ne!(b, a);
1118 assert_ne!(Ordering::Equal, b.cmp(&a));
1119 assert_ne!(Ordering::Equal, a.cmp(&b));
1120 assert_ne!(ah, bh);
1121 }
1122
1123 #[test]
1124 #[cfg_attr(
1125 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1126 wasm_bindgen_test
1127 )]
1128 fn test_default() {
1129 let default_uuid = Uuid::default();
1130 let nil_uuid = Uuid::nil();
1131
1132 assert_eq!(default_uuid, nil_uuid);
1133 }
1134
1135 #[test]
1136 #[cfg_attr(
1137 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1138 wasm_bindgen_test
1139 )]
1140 fn test_display() {
1141 use crate::std::fmt::Write;
1142
1143 for uuid in some_uuid_iter() {
1144 let s = uuid.to_string();
1145 let mut buffer = String::new();
1146
1147 assert_eq!(s, uuid.hyphenated().to_string());
1148
1149 check!(buffer, "{}", some_uuid_v4(), 36, |c| c.is_lowercase()
1150 || c.is_ascii_digit()
1151 || c == '-');
1152 }
1153 }
1154
1155 #[test]
1156 #[cfg_attr(
1157 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1158 wasm_bindgen_test
1159 )]
1160 fn test_to_simple_string() {
1161 for uuid in some_uuid_iter() {
1162 let s = uuid.simple().to_string();
1163
1164 assert_eq!(s.len(), 32);
1165 assert!(s.chars().all(|c| c.is_ascii_hexdigit()));
1166
1167 assert_eq!(Uuid::parse_str(&s).unwrap(), uuid);
1168 }
1169 }
1170
1171 #[test]
1172 #[cfg_attr(
1173 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1174 wasm_bindgen_test
1175 )]
1176 fn test_hyphenated_string() {
1177 for uuid in some_uuid_iter() {
1178 let s = uuid.hyphenated().to_string();
1179
1180 assert_eq!(36, s.len());
1181 assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
1182
1183 assert_eq!(Uuid::parse_str(&s).unwrap(), uuid);
1184 }
1185 }
1186
1187 #[test]
1188 #[cfg_attr(
1189 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1190 wasm_bindgen_test
1191 )]
1192 fn test_upper_lower_hex() {
1193 use std::fmt::Write;
1194
1195 let mut buf = String::new();
1196
1197 macro_rules! check {
1198 ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
1199 $buf.clear();
1200 write!($buf, $format, $target).unwrap();
1201 assert_eq!($len, buf.len());
1202 assert!($buf.chars().all($cond), "{}", $buf);
1203 };
1204 }
1205
1206 for uuid in some_uuid_iter() {
1207 check!(buf, "{:x}", uuid, 36, |c| c.is_lowercase()
1208 || c.is_ascii_digit()
1209 || c == '-');
1210 check!(buf, "{:X}", uuid, 36, |c| c.is_uppercase()
1211 || c.is_ascii_digit()
1212 || c == '-');
1213 check!(buf, "{:#x}", uuid, 36, |c| c.is_lowercase()
1214 || c.is_ascii_digit()
1215 || c == '-');
1216 check!(buf, "{:#X}", uuid, 36, |c| c.is_uppercase()
1217 || c.is_ascii_digit()
1218 || c == '-');
1219
1220 check!(buf, "{:X}", uuid.hyphenated(), 36, |c| c.is_uppercase()
1221 || c.is_ascii_digit()
1222 || c == '-');
1223 check!(buf, "{:X}", uuid.simple(), 32, |c| c.is_uppercase()
1224 || c.is_ascii_digit());
1225 check!(buf, "{:#X}", uuid.hyphenated(), 36, |c| c.is_uppercase()
1226 || c.is_ascii_digit()
1227 || c == '-');
1228 check!(buf, "{:#X}", uuid.simple(), 32, |c| c.is_uppercase()
1229 || c.is_ascii_digit());
1230
1231 check!(buf, "{:x}", uuid.hyphenated(), 36, |c| c.is_lowercase()
1232 || c.is_ascii_digit()
1233 || c == '-');
1234 check!(buf, "{:x}", uuid.simple(), 32, |c| c.is_lowercase()
1235 || c.is_ascii_digit());
1236 check!(buf, "{:#x}", uuid.hyphenated(), 36, |c| c.is_lowercase()
1237 || c.is_ascii_digit()
1238 || c == '-');
1239 check!(buf, "{:#x}", uuid.simple(), 32, |c| c.is_lowercase()
1240 || c.is_ascii_digit());
1241 }
1242 }
1243
1244 #[test]
1245 #[cfg_attr(
1246 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1247 wasm_bindgen_test
1248 )]
1249 fn test_to_urn_string() {
1250 for uuid in some_uuid_iter() {
1251 let ss = uuid.urn().to_string();
1252 let s = &ss[9..];
1253
1254 assert!(ss.starts_with("urn:uuid:"));
1255 assert_eq!(s.len(), 36);
1256 assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
1257
1258 assert_eq!(Uuid::parse_str(&ss).unwrap(), uuid);
1259 }
1260 }
1261
1262 #[test]
1263 #[cfg_attr(
1264 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1265 wasm_bindgen_test
1266 )]
1267 fn test_nil() {
1268 let nil = Uuid::nil();
1269 let not_nil = some_uuid_v4();
1270
1271 assert!(nil.is_nil());
1272 assert!(!not_nil.is_nil());
1273
1274 assert_eq!(nil.get_version(), Some(Version::Nil));
1275 assert_eq!(nil.get_variant(), Variant::NCS);
1276
1277 assert_eq!(not_nil.get_version(), Some(Version::Random));
1278
1279 assert_eq!(
1280 nil,
1281 Builder::from_bytes([0; 16])
1282 .with_version(Version::Nil)
1283 .into_uuid()
1284 );
1285 }
1286
1287 #[test]
1288 #[cfg_attr(
1289 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1290 wasm_bindgen_test
1291 )]
1292 fn test_max() {
1293 let max = Uuid::max();
1294 let not_max = some_uuid_v4();
1295
1296 assert!(max.is_max());
1297 assert!(!not_max.is_max());
1298
1299 assert_eq!(max.get_version(), Some(Version::Max));
1300 assert_eq!(max.get_variant(), Variant::Future);
1301
1302 assert_eq!(not_max.get_version(), Some(Version::Random));
1303
1304 assert_eq!(
1305 max,
1306 Builder::from_bytes([0xff; 16])
1307 .with_version(Version::Max)
1308 .into_uuid()
1309 );
1310 }
1311
1312 #[test]
1313 #[cfg_attr(
1314 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1315 wasm_bindgen_test
1316 )]
1317 fn test_predefined_namespaces() {
1318 assert_eq!(
1319 Uuid::NAMESPACE_DNS.hyphenated().to_string(),
1320 "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
1321 );
1322 assert_eq!(
1323 Uuid::NAMESPACE_URL.hyphenated().to_string(),
1324 "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
1325 );
1326 assert_eq!(
1327 Uuid::NAMESPACE_OID.hyphenated().to_string(),
1328 "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
1329 );
1330 assert_eq!(
1331 Uuid::NAMESPACE_X500.hyphenated().to_string(),
1332 "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
1333 );
1334 }
1335
1336 #[test]
1337 #[cfg_attr(
1338 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1339 wasm_bindgen_test
1340 )]
1341 fn test_get_timestamp_unsupported_version() {
1342 for uuid in [
1343 some_uuid_nil(),
1344 some_uuid_v3(),
1345 some_uuid_v4(),
1346 some_uuid_v5(),
1347 some_uuid_v8(),
1348 some_uuid_max(),
1349 ] {
1350 assert_ne!(Version::Mac, uuid.get_version().unwrap());
1351 assert_ne!(Version::SortMac, uuid.get_version().unwrap());
1352 assert_ne!(Version::SortRand, uuid.get_version().unwrap());
1353
1354 assert!(uuid.get_timestamp().is_none());
1355 }
1356 }
1357
1358 #[test]
1359 #[cfg_attr(
1360 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1361 wasm_bindgen_test
1362 )]
1363 fn test_get_node_id_unsupported_version() {
1364 for uuid in [
1365 some_uuid_nil(),
1366 some_uuid_v4(),
1367 some_uuid_v7(),
1368 some_uuid_v8(),
1369 some_uuid_max(),
1370 ] {
1371 assert_ne!(Version::Mac, uuid.get_version().unwrap());
1372 assert_ne!(Version::SortMac, uuid.get_version().unwrap());
1373
1374 assert!(uuid.get_node_id().is_none());
1375 }
1376 }
1377
1378 #[test]
1379 #[cfg_attr(
1380 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1381 wasm_bindgen_test
1382 )]
1383 fn test_get_version() {
1384 fn assert_version(uuid: Uuid, expected: Version) {
1385 assert_eq!(
1386 uuid.get_version().unwrap(),
1387 expected,
1388 "{uuid} version doesn't match {expected:?}"
1389 );
1390 assert_eq!(
1391 uuid.get_version_num(),
1392 expected as usize,
1393 "{uuid} version doesn't match {}",
1394 expected as usize
1395 );
1396 }
1397
1398 assert_version(some_uuid_nil(), Version::Nil);
1399 assert_version(some_uuid_v1(), Version::Mac);
1400 assert_version(some_uuid_v3(), Version::Md5);
1401 assert_version(some_uuid_v4(), Version::Random);
1402 assert_version(some_uuid_v5(), Version::Sha1);
1403 assert_version(some_uuid_v6(), Version::SortMac);
1404 assert_version(some_uuid_v7(), Version::SortRand);
1405 assert_version(some_uuid_v8(), Version::Custom);
1406 assert_version(some_uuid_max(), Version::Max);
1407 }
1408
1409 #[test]
1410 #[cfg_attr(
1411 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1412 wasm_bindgen_test
1413 )]
1414 fn test_get_version_non_conforming() {
1415 for case in [
1416 Uuid::from_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]),
1417 Uuid::parse_str("00000000-0000-0000-0000-00000000000f").unwrap(),
1418 Uuid::parse_str("ffffffff-ffff-ffff-ffff-fffffffffff0").unwrap(),
1419 ] {
1420 assert_eq!(case.get_version(), None);
1421 }
1422 }
1423
1424 #[test]
1425 #[cfg_attr(
1426 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1427 wasm_bindgen_test
1428 )]
1429 fn test_get_variant() {
1430 fn assert_variant(uuid: Uuid, expected: Variant) {
1431 assert_eq!(uuid.get_variant(), expected);
1432 }
1433
1434 for uuid in some_uuid_v_iter() {
1435 assert_variant(uuid, Variant::RFC4122);
1436 }
1437
1438 assert_variant(
1439 Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap(),
1440 Variant::Microsoft,
1441 );
1442 assert_variant(
1443 Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap(),
1444 Variant::Microsoft,
1445 );
1446 assert_variant(
1447 Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap(),
1448 Variant::NCS,
1449 );
1450 }
1451
1452 #[test]
1453 #[cfg_attr(
1454 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1455 wasm_bindgen_test
1456 )]
1457 fn test_from_fields() {
1458 let d1: u32 = 0xa1a2a3a4;
1459 let d2: u16 = 0xb1b2;
1460 let d3: u16 = 0xc1c2;
1461 let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1462
1463 let u = Uuid::from_fields(d1, d2, d3, &d4);
1464
1465 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1466 let result = u.simple().to_string();
1467 assert_eq!(result, expected);
1468 }
1469
1470 #[test]
1471 #[cfg_attr(
1472 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1473 wasm_bindgen_test
1474 )]
1475 fn test_from_fields_le() {
1476 let d1: u32 = 0xa4a3a2a1;
1477 let d2: u16 = 0xb2b1;
1478 let d3: u16 = 0xc2c1;
1479 let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1480
1481 let u = Uuid::from_fields_le(d1, d2, d3, &d4);
1482
1483 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1484 let result = u.simple().to_string();
1485 assert_eq!(result, expected);
1486 }
1487
1488 #[test]
1489 #[cfg_attr(
1490 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1491 wasm_bindgen_test
1492 )]
1493 fn test_fields_roundtrip() {
1494 let d1_in: u32 = 0xa1a2a3a4;
1495 let d2_in: u16 = 0xb1b2;
1496 let d3_in: u16 = 0xc1c2;
1497 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1498
1499 let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
1500 let (d1_out, d2_out, d3_out, d4_out) = u.as_fields();
1501
1502 assert_eq!(d1_in, d1_out);
1503 assert_eq!(d2_in, d2_out);
1504 assert_eq!(d3_in, d3_out);
1505 assert_eq!(d4_in, d4_out);
1506 }
1507
1508 #[test]
1509 #[cfg_attr(
1510 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1511 wasm_bindgen_test
1512 )]
1513 fn test_fields_le_roundtrip() {
1514 let d1_in: u32 = 0xa4a3a2a1;
1515 let d2_in: u16 = 0xb2b1;
1516 let d3_in: u16 = 0xc2c1;
1517 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1518
1519 let u = Uuid::from_fields_le(d1_in, d2_in, d3_in, d4_in);
1520 let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
1521
1522 assert_eq!(d1_in, d1_out);
1523 assert_eq!(d2_in, d2_out);
1524 assert_eq!(d3_in, d3_out);
1525 assert_eq!(d4_in, d4_out);
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_fields_le_are_actually_le() {
1534 let d1_in: u32 = 0xa1a2a3a4;
1535 let d2_in: u16 = 0xb1b2;
1536 let d3_in: u16 = 0xc1c2;
1537 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1538
1539 let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
1540 let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
1541
1542 assert_eq!(d1_in, d1_out.swap_bytes());
1543 assert_eq!(d2_in, d2_out.swap_bytes());
1544 assert_eq!(d3_in, d3_out.swap_bytes());
1545 assert_eq!(d4_in, d4_out);
1546 }
1547
1548 #[test]
1549 #[cfg_attr(
1550 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1551 wasm_bindgen_test
1552 )]
1553 fn test_u128_roundtrip() {
1554 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1555
1556 let u = Uuid::from_u128(v_in);
1557 let v_out = u.as_u128();
1558
1559 assert_eq!(v_in, v_out);
1560 }
1561
1562 #[test]
1563 #[cfg_attr(
1564 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1565 wasm_bindgen_test
1566 )]
1567 fn test_u128_le_roundtrip() {
1568 let v_in: u128 = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1;
1569
1570 let u = Uuid::from_u128_le(v_in);
1571 let v_out = u.to_u128_le();
1572
1573 assert_eq!(v_in, v_out);
1574 }
1575
1576 #[test]
1577 #[cfg_attr(
1578 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1579 wasm_bindgen_test
1580 )]
1581 fn test_u128_le_is_actually_le() {
1582 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1583
1584 let u = Uuid::from_u128(v_in);
1585 let v_out = u.to_u128_le();
1586
1587 assert_eq!(v_in, v_out.swap_bytes());
1588 }
1589
1590 #[test]
1591 #[cfg_attr(
1592 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1593 wasm_bindgen_test
1594 )]
1595 fn test_u64_pair_roundtrip() {
1596 let high_in: u64 = 0xa1a2a3a4b1b2c1c2;
1597 let low_in: u64 = 0xd1d2d3d4d5d6d7d8;
1598
1599 let u = Uuid::from_u64_pair(high_in, low_in);
1600 let (high_out, low_out) = u.as_u64_pair();
1601
1602 assert_eq!(high_in, high_out);
1603 assert_eq!(low_in, low_out);
1604 }
1605
1606 #[test]
1607 #[cfg_attr(
1608 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1609 wasm_bindgen_test
1610 )]
1611 fn test_from_slice() {
1612 let b = [
1613 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1614 0xd7, 0xd8,
1615 ];
1616
1617 let u = Uuid::from_slice(&b).unwrap();
1618 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1619
1620 assert_eq!(u.simple().to_string(), expected);
1621 }
1622
1623 #[test]
1624 #[cfg_attr(
1625 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1626 wasm_bindgen_test
1627 )]
1628 fn test_from_bytes() {
1629 let b = [
1630 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1631 0xd7, 0xd8,
1632 ];
1633
1634 let u = Uuid::from_bytes(b);
1635 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1636
1637 assert_eq!(u.simple().to_string(), expected);
1638 }
1639
1640 #[test]
1641 #[cfg_attr(
1642 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1643 wasm_bindgen_test
1644 )]
1645 fn test_as_bytes() {
1646 for uuid in some_uuid_v_iter() {
1647 let ub = uuid.as_bytes();
1648 let ur: &[u8] = uuid.as_ref();
1649
1650 assert_eq!(ub.len(), 16);
1651 assert_eq!(ur.len(), 16);
1652 assert!(!ub.iter().all(|&b| b == 0));
1653 assert!(!ur.iter().all(|&b| b == 0));
1654 }
1655 }
1656
1657 #[test]
1658 #[cfg(feature = "std")]
1659 #[cfg_attr(
1660 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1661 wasm_bindgen_test
1662 )]
1663 fn test_convert_vec() {
1664 for uuid in some_uuid_iter() {
1665 let ub: &[u8] = uuid.as_ref();
1666
1667 let v: std::vec::Vec<u8> = uuid.into();
1668
1669 assert_eq!(&v, ub);
1670
1671 let uv: Uuid = v.try_into().unwrap();
1672
1673 assert_eq!(uv, uuid);
1674 }
1675 }
1676
1677 #[test]
1678 #[cfg_attr(
1679 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1680 wasm_bindgen_test
1681 )]
1682 fn test_bytes_roundtrip() {
1683 let b_in: crate::Bytes = [
1684 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1685 0xd7, 0xd8,
1686 ];
1687
1688 let u = Uuid::from_slice(&b_in).unwrap();
1689
1690 let b_out = u.as_bytes();
1691
1692 assert_eq!(&b_in, b_out);
1693 }
1694
1695 #[test]
1696 #[cfg_attr(
1697 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1698 wasm_bindgen_test
1699 )]
1700 fn test_bytes_le_roundtrip() {
1701 let b = [
1702 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1703 0xd7, 0xd8,
1704 ];
1705
1706 let u1 = Uuid::from_bytes(b);
1707
1708 let b_le = u1.to_bytes_le();
1709
1710 let u2 = Uuid::from_bytes_le(b_le);
1711
1712 assert_eq!(u1, u2);
1713 }
1714}