1 use crate::prelude::*; 2 use winapi::shared::guiddef; 3 4 #[cfg(feature = "guid")] 5 impl Uuid { 6 /// Attempts to create a [`Uuid`] from a little endian winapi `GUID` 7 /// 8 /// [`Uuid`]: ../struct.Uuid.html from_guid(guid: guiddef::GUID) -> Result<Uuid, crate::Error>9 pub fn from_guid(guid: guiddef::GUID) -> Result<Uuid, crate::Error> { 10 Uuid::from_fields_le( 11 guid.Data1 as u32, 12 guid.Data2 as u16, 13 guid.Data3 as u16, 14 &(guid.Data4 as [u8; 8]), 15 ) 16 } 17 18 /// Converts a [`Uuid`] into a little endian winapi `GUID` 19 /// 20 /// [`Uuid`]: ../struct.Uuid.html to_guid(&self) -> guiddef::GUID21 pub fn to_guid(&self) -> guiddef::GUID { 22 let (data1, data2, data3, data4) = self.to_fields_le(); 23 24 guiddef::GUID { 25 Data1: data1, 26 Data2: data2, 27 Data3: data3, 28 Data4: *data4, 29 } 30 } 31 } 32 33 #[cfg(feature = "guid")] 34 #[cfg(test)] 35 mod tests { 36 use super::*; 37 38 use crate::std::string::ToString; 39 use winapi::shared::guiddef; 40 41 #[test] test_from_guid()42 fn test_from_guid() { 43 let guid = guiddef::GUID { 44 Data1: 0x4a35229d, 45 Data2: 0x5527, 46 Data3: 0x4f30, 47 Data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8], 48 }; 49 50 let uuid = Uuid::from_guid(guid).unwrap(); 51 assert_eq!( 52 "9d22354a-2755-304f-8647-9dc54e1ee1e8", 53 uuid.to_hyphenated().to_string() 54 ); 55 } 56 57 #[test] test_guid_roundtrip()58 fn test_guid_roundtrip() { 59 let guid_in = guiddef::GUID { 60 Data1: 0x4a35229d, 61 Data2: 0x5527, 62 Data3: 0x4f30, 63 Data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8], 64 }; 65 66 let uuid = Uuid::from_guid(guid_in).unwrap(); 67 let guid_out = uuid.to_guid(); 68 69 assert_eq!( 70 (guid_in.Data1, guid_in.Data2, guid_in.Data3, guid_in.Data4), 71 ( 72 guid_out.Data1, 73 guid_out.Data2, 74 guid_out.Data3, 75 guid_out.Data4 76 ) 77 ); 78 } 79 } 80