mirror of
https://github.com/uuid-rs/uuid.git
synced 2025-09-29 22:10:50 +00:00
Merge pull request #540 from KodrAus/fix/guid-convert
Don't flip GUID fields when converting to UUID
This commit is contained in:
commit
66a544d599
@ -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"]
|
||||
|
@ -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()
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user