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