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