From dbf0b2ecade96c9450aea876bff42918a8c1637a Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Sun, 31 Oct 2021 18:41:33 +1000 Subject: [PATCH] don't flip GUID fields when converting to UUID --- Cargo.toml | 4 ++++ src/winapi_support.rs | 48 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0fb1cc3..d8a4da8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -126,3 +126,7 @@ version = "0.2" [dev-dependencies.wasm-bindgen-test] version = "0.3" + +[target.'cfg(windows)'.dev-dependencies.winapi] +version = "0.3" +features = ["combaseapi"] diff --git a/src/winapi_support.rs b/src/winapi_support.rs index 8de2917..d6c3a83 100644 --- a/src/winapi_support.rs +++ b/src/winapi_support.rs @@ -4,11 +4,14 @@ use winapi::shared::guiddef; #[cfg(feature = "guid")] impl Uuid { - /// Converts a little endian winapi `GUID` into a [`Uuid`] + /// Converts a winapi `GUID` into a [`Uuid`] + /// + /// This method will pass fields unchanged, so they must already + /// be in the right endianness for a UUID. /// /// [`Uuid`]: ../struct.Uuid.html pub const fn from_guid(guid: guiddef::GUID) -> Self { - Uuid::from_fields_le( + Uuid::from_fields( guid.Data1 as u32, guid.Data2 as u16, guid.Data3 as u16, @@ -16,11 +19,14 @@ impl Uuid { ) } - /// Converts a [`Uuid`] into a little endian winapi `GUID` + /// Converts a [`Uuid`] into a winapi `GUID` + /// + /// This method will pass fields unchanged, so they must already + /// be in the right endianness for a UUID. /// /// [`Uuid`]: ../struct.Uuid.html pub fn to_guid(&self) -> guiddef::GUID { - let (data1, data2, data3, data4) = self.to_fields_le(); + let (data1, data2, data3, data4) = self.as_fields(); guiddef::GUID { Data1: data1, @@ -36,8 +42,36 @@ impl Uuid { mod tests { use super::*; - use crate::std::string::ToString; - use winapi::shared::guiddef; + use crate::{std::string::ToString, Variant, Version}; + use winapi::{shared::guiddef, um::combaseapi::CoCreateGuid}; + + #[test] + fn test_parse_guid() { + // This example GUID is directly from https://docs.microsoft.com/en-us/windows/win32/api/guiddef/ns-guiddef-guid + let uuid = Uuid::parse_str("6B29FC40-CA47-1067-B31D-00DD010662DA").unwrap(); + + assert_eq!(Variant::RFC4122, uuid.get_variant()); + assert_eq!(Some(Version::Mac), uuid.get_version()); + } + + #[test] + fn test_new_native_guid() { + let mut guid = guiddef::GUID { + Data1: Default::default(), + Data2: Default::default(), + Data3: Default::default(), + Data4: Default::default(), + }; + + unsafe { + CoCreateGuid(&mut guid as *mut _); + } + + let uuid = Uuid::from_guid(guid); + + assert_eq!(Variant::RFC4122, uuid.get_variant()); + assert_eq!(Some(Version::Random), uuid.get_version()); + } #[test] fn test_from_guid() { @@ -50,7 +84,7 @@ mod tests { let uuid = Uuid::from_guid(guid); assert_eq!( - "9d22354a-2755-304f-8647-9dc54e1ee1e8", + "4a35229d-5527-4f30-8647-9dc54e1ee1e8", uuid.to_hyphenated().to_string() ); }