1#![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.19.0"
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"))]
241pub use timestamp::context::Context;
242
243#[cfg(feature = "v7")]
244pub use timestamp::context::ContextV7;
245
246#[cfg(feature = "v1")]
247#[doc(hidden)]
248pub mod v1;
251#[cfg(feature = "v3")]
252mod v3;
253#[cfg(feature = "v4")]
254mod v4;
255#[cfg(feature = "v5")]
256mod v5;
257#[cfg(feature = "v6")]
258mod v6;
259#[cfg(feature = "v7")]
260mod v7;
261#[cfg(feature = "v8")]
262mod v8;
263
264#[cfg(feature = "md5")]
265mod md5;
266#[cfg(feature = "rng")]
267mod rng;
268#[cfg(feature = "sha1")]
269mod sha1;
270
271mod external;
272
273#[doc(hidden)]
274#[cfg(feature = "macro-diagnostics")]
275pub extern crate uuid_macro_internal;
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]
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)]
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 = 0xff,
320}
321
322#[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)]
328#[non_exhaustive]
329#[repr(u8)]
330pub enum Variant {
331 NCS = 0u8,
333 RFC4122,
336 Microsoft,
338 Future,
340}
341
342#[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_receiver_is_total_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)]
437#[repr(transparent)]
438#[cfg_attr(
440 feature = "borsh",
441 derive(borsh_derive::BorshDeserialize, borsh_derive::BorshSerialize)
442)]
443#[cfg_attr(
444 feature = "bytemuck",
445 derive(bytemuck::Zeroable, bytemuck::Pod, bytemuck::TransparentWrapper)
446)]
447#[cfg_attr(
448 all(uuid_unstable, feature = "zerocopy"),
449 derive(
450 zerocopy::IntoBytes,
451 zerocopy::FromBytes,
452 zerocopy::KnownLayout,
453 zerocopy::Immutable,
454 zerocopy::Unaligned
455 )
456)]
457pub struct Uuid(Bytes);
458
459impl Uuid {
460 pub const NAMESPACE_DNS: Self = Uuid([
462 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
463 0xc8,
464 ]);
465
466 pub const NAMESPACE_OID: Self = Uuid([
468 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
469 0xc8,
470 ]);
471
472 pub const NAMESPACE_URL: Self = Uuid([
474 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
475 0xc8,
476 ]);
477
478 pub const NAMESPACE_X500: Self = Uuid([
480 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
481 0xc8,
482 ]);
483
484 pub const fn get_variant(&self) -> Variant {
508 match self.as_bytes()[8] {
509 x if x & 0x80 == 0x00 => Variant::NCS,
510 x if x & 0xc0 == 0x80 => Variant::RFC4122,
511 x if x & 0xe0 == 0xc0 => Variant::Microsoft,
512 x if x & 0xe0 == 0xe0 => Variant::Future,
513 _ => Variant::Future,
517 }
518 }
519
520 pub const fn get_version_num(&self) -> usize {
543 (self.as_bytes()[6] >> 4) as usize
544 }
545
546 pub const fn get_version(&self) -> Option<Version> {
573 match self.get_version_num() {
574 0 if self.is_nil() => Some(Version::Nil),
575 1 => Some(Version::Mac),
576 2 => Some(Version::Dce),
577 3 => Some(Version::Md5),
578 4 => Some(Version::Random),
579 5 => Some(Version::Sha1),
580 6 => Some(Version::SortMac),
581 7 => Some(Version::SortRand),
582 8 => Some(Version::Custom),
583 0xf => Some(Version::Max),
584 _ => None,
585 }
586 }
587
588 pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8]) {
632 let bytes = self.as_bytes();
633
634 let d1 = (bytes[0] as u32) << 24
635 | (bytes[1] as u32) << 16
636 | (bytes[2] as u32) << 8
637 | (bytes[3] as u32);
638
639 let d2 = (bytes[4] as u16) << 8 | (bytes[5] as u16);
640
641 let d3 = (bytes[6] as u16) << 8 | (bytes[7] as u16);
642
643 let d4: &[u8; 8] = bytes[8..16].try_into().unwrap();
644 (d1, d2, d3, d4)
645 }
646
647 pub fn to_fields_le(&self) -> (u32, u16, u16, &[u8; 8]) {
675 let d1 = (self.as_bytes()[0] as u32)
676 | (self.as_bytes()[1] as u32) << 8
677 | (self.as_bytes()[2] as u32) << 16
678 | (self.as_bytes()[3] as u32) << 24;
679
680 let d2 = (self.as_bytes()[4] as u16) | (self.as_bytes()[5] as u16) << 8;
681
682 let d3 = (self.as_bytes()[6] as u16) | (self.as_bytes()[7] as u16) << 8;
683
684 let d4: &[u8; 8] = self.as_bytes()[8..16].try_into().unwrap();
685 (d1, d2, d3, d4)
686 }
687
688 pub const fn as_u128(&self) -> u128 {
707 u128::from_be_bytes(*self.as_bytes())
708 }
709
710 pub const fn to_u128_le(&self) -> u128 {
736 u128::from_le_bytes(*self.as_bytes())
737 }
738
739 pub const fn as_u64_pair(&self) -> (u64, u64) {
759 let value = self.as_u128();
760 ((value >> 64) as u64, value as u64)
761 }
762
763 #[inline]
790 pub const fn as_bytes(&self) -> &Bytes {
791 &self.0
792 }
793
794 #[inline]
810 pub const fn into_bytes(self) -> Bytes {
811 self.0
812 }
813
814 pub const fn to_bytes_le(&self) -> Bytes {
839 [
840 self.0[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4], self.0[7], self.0[6],
841 self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14],
842 self.0[15],
843 ]
844 }
845
846 pub const fn is_nil(&self) -> bool {
848 self.as_u128() == u128::MIN
849 }
850
851 pub const fn is_max(&self) -> bool {
853 self.as_u128() == u128::MAX
854 }
855
856 pub const fn encode_buffer() -> [u8; fmt::Urn::LENGTH] {
882 [0; fmt::Urn::LENGTH]
883 }
884
885 pub const fn get_timestamp(&self) -> Option<Timestamp> {
895 match self.get_version() {
896 Some(Version::Mac) => {
897 let (ticks, counter) = timestamp::decode_gregorian_timestamp(self);
898
899 Some(Timestamp::from_gregorian(ticks, counter))
900 }
901 Some(Version::SortMac) => {
902 let (ticks, counter) = timestamp::decode_sorted_gregorian_timestamp(self);
903
904 Some(Timestamp::from_gregorian(ticks, counter))
905 }
906 Some(Version::SortRand) => {
907 let millis = timestamp::decode_unix_timestamp_millis(self);
908
909 let seconds = millis / 1000;
910 let nanos = ((millis % 1000) * 1_000_000) as u32;
911
912 Some(Timestamp::from_unix_time(seconds, nanos, 0, 0))
913 }
914 _ => None,
915 }
916 }
917
918 pub const fn get_node_id(&self) -> Option<[u8; 6]> {
921 match self.get_version() {
922 Some(Version::Mac) | Some(Version::SortMac) => {
923 let mut node_id = [0; 6];
924
925 node_id[0] = self.0[10];
926 node_id[1] = self.0[11];
927 node_id[2] = self.0[12];
928 node_id[3] = self.0[13];
929 node_id[4] = self.0[14];
930 node_id[5] = self.0[15];
931
932 Some(node_id)
933 }
934 _ => None,
935 }
936 }
937}
938
939impl Hash for Uuid {
940 fn hash<H: Hasher>(&self, state: &mut H) {
941 state.write(&self.0);
942 }
943}
944
945impl Default for Uuid {
946 #[inline]
947 fn default() -> Self {
948 Uuid::nil()
949 }
950}
951
952impl AsRef<Uuid> for Uuid {
953 #[inline]
954 fn as_ref(&self) -> &Uuid {
955 self
956 }
957}
958
959impl AsRef<[u8]> for Uuid {
960 #[inline]
961 fn as_ref(&self) -> &[u8] {
962 &self.0
963 }
964}
965
966#[cfg(feature = "std")]
967impl From<Uuid> for std::vec::Vec<u8> {
968 fn from(value: Uuid) -> Self {
969 value.0.to_vec()
970 }
971}
972
973#[cfg(feature = "std")]
974impl TryFrom<std::vec::Vec<u8>> for Uuid {
975 type Error = Error;
976
977 fn try_from(value: std::vec::Vec<u8>) -> Result<Self, Self::Error> {
978 Uuid::from_slice(&value)
979 }
980}
981
982#[cfg(feature = "serde")]
983pub mod serde {
984 pub use crate::external::serde_support::{braced, compact, simple, urn};
991}
992
993#[cfg(test)]
994mod tests {
995 use super::*;
996
997 use crate::std::string::{String, ToString};
998
999 #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
1000 use wasm_bindgen_test::*;
1001
1002 macro_rules! check {
1003 ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
1004 $buf.clear();
1005 write!($buf, $format, $target).unwrap();
1006 assert!($buf.len() == $len);
1007 assert!($buf.chars().all($cond), "{}", $buf);
1008 };
1009 }
1010
1011 pub const fn new() -> Uuid {
1012 Uuid::from_bytes([
1013 0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
1014 0xA1, 0xE4,
1015 ])
1016 }
1017
1018 pub const fn new2() -> Uuid {
1019 Uuid::from_bytes([
1020 0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
1021 0xA1, 0xE4,
1022 ])
1023 }
1024
1025 #[test]
1026 #[cfg_attr(
1027 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1028 wasm_bindgen_test
1029 )]
1030 fn test_uuid_compare() {
1031 let uuid1 = new();
1032 let uuid2 = new2();
1033
1034 assert_eq!(uuid1, uuid1);
1035 assert_eq!(uuid2, uuid2);
1036
1037 assert_ne!(uuid1, uuid2);
1038 assert_ne!(uuid2, uuid1);
1039 }
1040
1041 #[test]
1042 #[cfg_attr(
1043 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1044 wasm_bindgen_test
1045 )]
1046 fn test_uuid_default() {
1047 let default_uuid = Uuid::default();
1048 let nil_uuid = Uuid::nil();
1049
1050 assert_eq!(default_uuid, nil_uuid);
1051 }
1052
1053 #[test]
1054 #[cfg_attr(
1055 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1056 wasm_bindgen_test
1057 )]
1058 fn test_uuid_display() {
1059 use crate::std::fmt::Write;
1060
1061 let uuid = new();
1062 let s = uuid.to_string();
1063 let mut buffer = String::new();
1064
1065 assert_eq!(s, uuid.hyphenated().to_string());
1066
1067 check!(buffer, "{}", uuid, 36, |c| c.is_lowercase()
1068 || c.is_ascii_digit()
1069 || c == '-');
1070 }
1071
1072 #[test]
1073 #[cfg_attr(
1074 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1075 wasm_bindgen_test
1076 )]
1077 fn test_uuid_lowerhex() {
1078 use crate::std::fmt::Write;
1079
1080 let mut buffer = String::new();
1081 let uuid = new();
1082
1083 check!(buffer, "{:x}", uuid, 36, |c| c.is_lowercase()
1084 || c.is_ascii_digit()
1085 || c == '-');
1086 }
1087
1088 #[test]
1090 #[cfg_attr(
1091 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1092 wasm_bindgen_test
1093 )]
1094 fn test_uuid_operator_eq() {
1095 let uuid1 = new();
1096 let uuid1_dup = uuid1;
1097 let uuid2 = new2();
1098
1099 assert!(uuid1 == uuid1);
1100 assert!(uuid1 == uuid1_dup);
1101 assert!(uuid1_dup == uuid1);
1102
1103 assert!(uuid1 != uuid2);
1104 assert!(uuid2 != uuid1);
1105 assert!(uuid1_dup != uuid2);
1106 assert!(uuid2 != uuid1_dup);
1107 }
1108
1109 #[test]
1110 #[cfg_attr(
1111 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1112 wasm_bindgen_test
1113 )]
1114 fn test_uuid_to_string() {
1115 use crate::std::fmt::Write;
1116
1117 let uuid = new();
1118 let s = uuid.to_string();
1119 let mut buffer = String::new();
1120
1121 assert_eq!(s.len(), 36);
1122
1123 check!(buffer, "{}", s, 36, |c| c.is_lowercase()
1124 || c.is_ascii_digit()
1125 || c == '-');
1126 }
1127
1128 #[test]
1129 #[cfg_attr(
1130 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1131 wasm_bindgen_test
1132 )]
1133 fn test_non_conforming() {
1134 let from_bytes =
1135 Uuid::from_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]);
1136
1137 assert_eq!(from_bytes.get_version(), None);
1138 }
1139
1140 #[test]
1141 #[cfg_attr(
1142 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1143 wasm_bindgen_test
1144 )]
1145 fn test_nil() {
1146 let nil = Uuid::nil();
1147 let not_nil = new();
1148
1149 assert!(nil.is_nil());
1150 assert!(!not_nil.is_nil());
1151
1152 assert_eq!(nil.get_version(), Some(Version::Nil));
1153 assert_eq!(not_nil.get_version(), Some(Version::Random));
1154
1155 assert_eq!(
1156 nil,
1157 Builder::from_bytes([0; 16])
1158 .with_version(Version::Nil)
1159 .into_uuid()
1160 );
1161 }
1162
1163 #[test]
1164 #[cfg_attr(
1165 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1166 wasm_bindgen_test
1167 )]
1168 fn test_max() {
1169 let max = Uuid::max();
1170 let not_max = new();
1171
1172 assert!(max.is_max());
1173 assert!(!not_max.is_max());
1174
1175 assert_eq!(max.get_version(), Some(Version::Max));
1176 assert_eq!(not_max.get_version(), Some(Version::Random));
1177
1178 assert_eq!(
1179 max,
1180 Builder::from_bytes([0xff; 16])
1181 .with_version(Version::Max)
1182 .into_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_predefined_namespaces() {
1192 assert_eq!(
1193 Uuid::NAMESPACE_DNS.hyphenated().to_string(),
1194 "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
1195 );
1196 assert_eq!(
1197 Uuid::NAMESPACE_URL.hyphenated().to_string(),
1198 "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
1199 );
1200 assert_eq!(
1201 Uuid::NAMESPACE_OID.hyphenated().to_string(),
1202 "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
1203 );
1204 assert_eq!(
1205 Uuid::NAMESPACE_X500.hyphenated().to_string(),
1206 "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
1207 );
1208 }
1209
1210 #[cfg(feature = "v3")]
1211 #[test]
1212 #[cfg_attr(
1213 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1214 wasm_bindgen_test
1215 )]
1216 fn test_get_version_v3() {
1217 let uuid = Uuid::new_v3(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
1218
1219 assert_eq!(uuid.get_version().unwrap(), Version::Md5);
1220 assert_eq!(uuid.get_version_num(), 3);
1221 }
1222
1223 #[test]
1224 #[cfg_attr(
1225 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1226 wasm_bindgen_test
1227 )]
1228 fn test_get_timestamp_unsupported_version() {
1229 let uuid = new();
1230
1231 assert_ne!(Version::Mac, uuid.get_version().unwrap());
1232 assert_ne!(Version::SortMac, uuid.get_version().unwrap());
1233 assert_ne!(Version::SortRand, uuid.get_version().unwrap());
1234
1235 assert!(uuid.get_timestamp().is_none());
1236 }
1237
1238 #[test]
1239 #[cfg_attr(
1240 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1241 wasm_bindgen_test
1242 )]
1243 fn test_get_node_id_unsupported_version() {
1244 let uuid = new();
1245
1246 assert_ne!(Version::Mac, uuid.get_version().unwrap());
1247 assert_ne!(Version::SortMac, uuid.get_version().unwrap());
1248
1249 assert!(uuid.get_node_id().is_none());
1250 }
1251
1252 #[test]
1253 #[cfg_attr(
1254 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1255 wasm_bindgen_test
1256 )]
1257 fn test_get_variant() {
1258 let uuid1 = new();
1259 let uuid2 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
1260 let uuid3 = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
1261 let uuid4 = Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap();
1262 let uuid5 = Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap();
1263 let uuid6 = Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap();
1264
1265 assert_eq!(uuid1.get_variant(), Variant::RFC4122);
1266 assert_eq!(uuid2.get_variant(), Variant::RFC4122);
1267 assert_eq!(uuid3.get_variant(), Variant::RFC4122);
1268 assert_eq!(uuid4.get_variant(), Variant::Microsoft);
1269 assert_eq!(uuid5.get_variant(), Variant::Microsoft);
1270 assert_eq!(uuid6.get_variant(), Variant::NCS);
1271 }
1272
1273 #[test]
1274 #[cfg_attr(
1275 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1276 wasm_bindgen_test
1277 )]
1278 fn test_to_simple_string() {
1279 let uuid1 = new();
1280 let s = uuid1.simple().to_string();
1281
1282 assert_eq!(s.len(), 32);
1283 assert!(s.chars().all(|c| c.is_ascii_hexdigit()));
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_hyphenated_string() {
1292 let uuid1 = new();
1293 let s = uuid1.hyphenated().to_string();
1294
1295 assert_eq!(36, s.len());
1296 assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
1297 }
1298
1299 #[test]
1300 #[cfg_attr(
1301 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1302 wasm_bindgen_test
1303 )]
1304 fn test_upper_lower_hex() {
1305 use std::fmt::Write;
1306
1307 let mut buf = String::new();
1308 let u = new();
1309
1310 macro_rules! check {
1311 ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
1312 $buf.clear();
1313 write!($buf, $format, $target).unwrap();
1314 assert_eq!($len, buf.len());
1315 assert!($buf.chars().all($cond), "{}", $buf);
1316 };
1317 }
1318
1319 check!(buf, "{:x}", u, 36, |c| c.is_lowercase()
1320 || c.is_ascii_digit()
1321 || c == '-');
1322 check!(buf, "{:X}", u, 36, |c| c.is_uppercase()
1323 || c.is_ascii_digit()
1324 || c == '-');
1325 check!(buf, "{:#x}", u, 36, |c| c.is_lowercase()
1326 || c.is_ascii_digit()
1327 || c == '-');
1328 check!(buf, "{:#X}", u, 36, |c| c.is_uppercase()
1329 || c.is_ascii_digit()
1330 || c == '-');
1331
1332 check!(buf, "{:X}", u.hyphenated(), 36, |c| c.is_uppercase()
1333 || c.is_ascii_digit()
1334 || c == '-');
1335 check!(buf, "{:X}", u.simple(), 32, |c| c.is_uppercase()
1336 || c.is_ascii_digit());
1337 check!(buf, "{:#X}", u.hyphenated(), 36, |c| c.is_uppercase()
1338 || c.is_ascii_digit()
1339 || c == '-');
1340 check!(buf, "{:#X}", u.simple(), 32, |c| c.is_uppercase()
1341 || c.is_ascii_digit());
1342
1343 check!(buf, "{:x}", u.hyphenated(), 36, |c| c.is_lowercase()
1344 || c.is_ascii_digit()
1345 || c == '-');
1346 check!(buf, "{:x}", u.simple(), 32, |c| c.is_lowercase()
1347 || c.is_ascii_digit());
1348 check!(buf, "{:#x}", u.hyphenated(), 36, |c| c.is_lowercase()
1349 || c.is_ascii_digit()
1350 || c == '-');
1351 check!(buf, "{:#x}", u.simple(), 32, |c| c.is_lowercase()
1352 || c.is_ascii_digit());
1353 }
1354
1355 #[test]
1356 #[cfg_attr(
1357 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1358 wasm_bindgen_test
1359 )]
1360 fn test_to_urn_string() {
1361 let uuid1 = new();
1362 let ss = uuid1.urn().to_string();
1363 let s = &ss[9..];
1364
1365 assert!(ss.starts_with("urn:uuid:"));
1366 assert_eq!(s.len(), 36);
1367 assert!(s.chars().all(|c| c.is_ascii_hexdigit() || c == '-'));
1368 }
1369
1370 #[test]
1371 #[cfg_attr(
1372 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1373 wasm_bindgen_test
1374 )]
1375 fn test_to_simple_string_matching() {
1376 let uuid1 = new();
1377
1378 let hs = uuid1.hyphenated().to_string();
1379 let ss = uuid1.simple().to_string();
1380
1381 let hsn = hs.chars().filter(|&c| c != '-').collect::<String>();
1382
1383 assert_eq!(hsn, ss);
1384 }
1385
1386 #[test]
1387 #[cfg_attr(
1388 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1389 wasm_bindgen_test
1390 )]
1391 fn test_string_roundtrip() {
1392 let uuid = new();
1393
1394 let hs = uuid.hyphenated().to_string();
1395 let uuid_hs = Uuid::parse_str(&hs).unwrap();
1396 assert_eq!(uuid_hs, uuid);
1397
1398 let ss = uuid.to_string();
1399 let uuid_ss = Uuid::parse_str(&ss).unwrap();
1400 assert_eq!(uuid_ss, uuid);
1401 }
1402
1403 #[test]
1404 #[cfg_attr(
1405 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1406 wasm_bindgen_test
1407 )]
1408 fn test_from_fields() {
1409 let d1: u32 = 0xa1a2a3a4;
1410 let d2: u16 = 0xb1b2;
1411 let d3: u16 = 0xc1c2;
1412 let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1413
1414 let u = Uuid::from_fields(d1, d2, d3, &d4);
1415
1416 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1417 let result = u.simple().to_string();
1418 assert_eq!(result, expected);
1419 }
1420
1421 #[test]
1422 #[cfg_attr(
1423 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1424 wasm_bindgen_test
1425 )]
1426 fn test_from_fields_le() {
1427 let d1: u32 = 0xa4a3a2a1;
1428 let d2: u16 = 0xb2b1;
1429 let d3: u16 = 0xc2c1;
1430 let d4 = [0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1431
1432 let u = Uuid::from_fields_le(d1, d2, d3, &d4);
1433
1434 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1435 let result = u.simple().to_string();
1436 assert_eq!(result, expected);
1437 }
1438
1439 #[test]
1440 #[cfg_attr(
1441 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1442 wasm_bindgen_test
1443 )]
1444 fn test_as_fields() {
1445 let u = new();
1446 let (d1, d2, d3, d4) = u.as_fields();
1447
1448 assert_ne!(d1, 0);
1449 assert_ne!(d2, 0);
1450 assert_ne!(d3, 0);
1451 assert_eq!(d4.len(), 8);
1452 assert!(!d4.iter().all(|&b| b == 0));
1453 }
1454
1455 #[test]
1456 #[cfg_attr(
1457 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1458 wasm_bindgen_test
1459 )]
1460 fn test_fields_roundtrip() {
1461 let d1_in: u32 = 0xa1a2a3a4;
1462 let d2_in: u16 = 0xb1b2;
1463 let d3_in: u16 = 0xc1c2;
1464 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1465
1466 let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
1467 let (d1_out, d2_out, d3_out, d4_out) = u.as_fields();
1468
1469 assert_eq!(d1_in, d1_out);
1470 assert_eq!(d2_in, d2_out);
1471 assert_eq!(d3_in, d3_out);
1472 assert_eq!(d4_in, d4_out);
1473 }
1474
1475 #[test]
1476 #[cfg_attr(
1477 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1478 wasm_bindgen_test
1479 )]
1480 fn test_fields_le_roundtrip() {
1481 let d1_in: u32 = 0xa4a3a2a1;
1482 let d2_in: u16 = 0xb2b1;
1483 let d3_in: u16 = 0xc2c1;
1484 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1485
1486 let u = Uuid::from_fields_le(d1_in, d2_in, d3_in, d4_in);
1487 let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
1488
1489 assert_eq!(d1_in, d1_out);
1490 assert_eq!(d2_in, d2_out);
1491 assert_eq!(d3_in, d3_out);
1492 assert_eq!(d4_in, d4_out);
1493 }
1494
1495 #[test]
1496 #[cfg_attr(
1497 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1498 wasm_bindgen_test
1499 )]
1500 fn test_fields_le_are_actually_le() {
1501 let d1_in: u32 = 0xa1a2a3a4;
1502 let d2_in: u16 = 0xb1b2;
1503 let d3_in: u16 = 0xc1c2;
1504 let d4_in = &[0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8];
1505
1506 let u = Uuid::from_fields(d1_in, d2_in, d3_in, d4_in);
1507 let (d1_out, d2_out, d3_out, d4_out) = u.to_fields_le();
1508
1509 assert_eq!(d1_in, d1_out.swap_bytes());
1510 assert_eq!(d2_in, d2_out.swap_bytes());
1511 assert_eq!(d3_in, d3_out.swap_bytes());
1512 assert_eq!(d4_in, d4_out);
1513 }
1514
1515 #[test]
1516 #[cfg_attr(
1517 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1518 wasm_bindgen_test
1519 )]
1520 fn test_from_u128() {
1521 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1522
1523 let u = Uuid::from_u128(v_in);
1524
1525 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1526 let result = u.simple().to_string();
1527 assert_eq!(result, expected);
1528 }
1529
1530 #[test]
1531 #[cfg_attr(
1532 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1533 wasm_bindgen_test
1534 )]
1535 fn test_from_u128_le() {
1536 let v_in: u128 = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1;
1537
1538 let u = Uuid::from_u128_le(v_in);
1539
1540 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1541 let result = u.simple().to_string();
1542 assert_eq!(result, expected);
1543 }
1544
1545 #[test]
1546 #[cfg_attr(
1547 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1548 wasm_bindgen_test
1549 )]
1550 fn test_from_u64_pair() {
1551 let high_in: u64 = 0xa1a2a3a4b1b2c1c2;
1552 let low_in: u64 = 0xd1d2d3d4d5d6d7d8;
1553
1554 let u = Uuid::from_u64_pair(high_in, low_in);
1555
1556 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1557 let result = u.simple().to_string();
1558 assert_eq!(result, expected);
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_roundtrip() {
1567 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1568
1569 let u = Uuid::from_u128(v_in);
1570 let v_out = u.as_u128();
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_roundtrip() {
1581 let v_in: u128 = 0xd8d7d6d5d4d3d2d1c2c1b2b1a4a3a2a1;
1582
1583 let u = Uuid::from_u128_le(v_in);
1584 let v_out = u.to_u128_le();
1585
1586 assert_eq!(v_in, v_out);
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_u128_le_is_actually_le() {
1611 let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
1612
1613 let u = Uuid::from_u128(v_in);
1614 let v_out = u.to_u128_le();
1615
1616 assert_eq!(v_in, v_out.swap_bytes());
1617 }
1618
1619 #[test]
1620 #[cfg_attr(
1621 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1622 wasm_bindgen_test
1623 )]
1624 fn test_from_slice() {
1625 let b = [
1626 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1627 0xd7, 0xd8,
1628 ];
1629
1630 let u = Uuid::from_slice(&b).unwrap();
1631 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1632
1633 assert_eq!(u.simple().to_string(), expected);
1634 }
1635
1636 #[test]
1637 #[cfg_attr(
1638 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1639 wasm_bindgen_test
1640 )]
1641 fn test_from_bytes() {
1642 let b = [
1643 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1644 0xd7, 0xd8,
1645 ];
1646
1647 let u = Uuid::from_bytes(b);
1648 let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
1649
1650 assert_eq!(u.simple().to_string(), expected);
1651 }
1652
1653 #[test]
1654 #[cfg_attr(
1655 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1656 wasm_bindgen_test
1657 )]
1658 fn test_as_bytes() {
1659 let u = new();
1660 let ub = u.as_bytes();
1661 let ur: &[u8] = u.as_ref();
1662
1663 assert_eq!(ub.len(), 16);
1664 assert_eq!(ur.len(), 16);
1665 assert!(!ub.iter().all(|&b| b == 0));
1666 assert!(!ur.iter().all(|&b| b == 0));
1667 }
1668
1669 #[test]
1670 #[cfg(feature = "std")]
1671 #[cfg_attr(
1672 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1673 wasm_bindgen_test
1674 )]
1675 fn test_convert_vec() {
1676 let u = new();
1677 let ub: &[u8] = u.as_ref();
1678
1679 let v: std::vec::Vec<u8> = u.into();
1680
1681 assert_eq!(&v, ub);
1682
1683 let uv: Uuid = v.try_into().unwrap();
1684
1685 assert_eq!(uv, u);
1686 }
1687
1688 #[test]
1689 #[cfg_attr(
1690 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1691 wasm_bindgen_test
1692 )]
1693 fn test_bytes_roundtrip() {
1694 let b_in: crate::Bytes = [
1695 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1696 0xd7, 0xd8,
1697 ];
1698
1699 let u = Uuid::from_slice(&b_in).unwrap();
1700
1701 let b_out = u.as_bytes();
1702
1703 assert_eq!(&b_in, b_out);
1704 }
1705
1706 #[test]
1707 #[cfg_attr(
1708 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1709 wasm_bindgen_test
1710 )]
1711 fn test_bytes_le_roundtrip() {
1712 let b = [
1713 0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
1714 0xd7, 0xd8,
1715 ];
1716
1717 let u1 = Uuid::from_bytes(b);
1718
1719 let b_le = u1.to_bytes_le();
1720
1721 let u2 = Uuid::from_bytes_le(b_le);
1722
1723 assert_eq!(u1, u2);
1724 }
1725
1726 #[test]
1727 #[cfg_attr(
1728 all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
1729 wasm_bindgen_test
1730 )]
1731 fn test_iterbytes_impl_for_uuid() {
1732 let mut set = std::collections::HashSet::new();
1733 let id1 = new();
1734 let id2 = new2();
1735 set.insert(id1);
1736
1737 assert!(set.contains(&id1));
1738 assert!(!set.contains(&id2));
1739 }
1740}