Debian Codegraph @

third_party/rust/uuid/examples/windows_guid.rs

raw
1 //! Converting between Windows GUIDs and UUIDs.
2 //!
3 //! Windows GUIDs are specified as using mixed endianness.
4 //! What you get will depend on the source of the GUID.
5 //! Functions like `CoCreateGuid` will generate a valid UUID so
6 //! the fields will be naturally ordered for `Uuid::from_fields`.
7 //! Other GUIDs might need to be passed to `Uuid::from_fields_le`
8 //! to have their ordering swapped.
9
10 #[test]
11 #[cfg(windows)]
12 fn guid_to_uuid() {
13 use uuid::Uuid;
14 use windows_sys::core;
15
16 let guid_in = core::GUID {
17 data1: 0x4a35229d,
18 data2: 0x5527,
19 data3: 0x4f30,
20 data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8],
21 };
22
23 let uuid = Uuid::from_fields(guid_in.data1, guid_in.data2, guid_in.data3, &guid_in.data4);
24
25 let guid_out = {
26 let fields = uuid.as_fields();
27
28 core::GUID {
29 data1: fields.0,
30 data2: fields.1,
31 data3: fields.2,
32 data4: *fields.3,
33 }
34 };
35
36 assert_eq!(
37 (guid_in.data1, guid_in.data2, guid_in.data3, guid_in.data4),
38 (
39 guid_out.data1,
40 guid_out.data2,
41 guid_out.data3,
42 guid_out.data4
43 )
44 );
45 }
46
47 #[test]
48 #[cfg(windows)]
49 fn guid_to_uuid_le_encoded() {
50 use uuid::Uuid;
51 use windows_sys::core;
52
53 // A GUID might not be encoded directly as a UUID
54 // If its fields are stored in little-endian order they might
55 // need to be flipped. Whether or not this is necessary depends
56 // on the source of the GUID
57 let guid_in = core::GUID {
58 data1: 0x9d22354a,
59 data2: 0x2755,
60 data3: 0x304f,
61 data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8],
62 };
63
64 let uuid = Uuid::from_fields_le(guid_in.data1, guid_in.data2, guid_in.data3, &guid_in.data4);
65
66 let guid_out = {
67 let fields = uuid.to_fields_le();
68
69 core::GUID {
70 data1: fields.0,
71 data2: fields.1,
72 data3: fields.2,
73 data4: *fields.3,
74 }
75 };
76
77 assert_eq!(
78 (guid_in.data1, guid_in.data2, guid_in.data3, guid_in.data4),
79 (
80 guid_out.data1,
81 guid_out.data2,
82 guid_out.data3,
83 guid_out.data4
84 )
85 );
86 }
87
88 #[test]
89 #[cfg(windows)]
90 fn uuid_from_cocreateguid() {
91 use uuid::{Uuid, Variant, Version};
92 use windows_sys::core;
93 use windows_sys::Win32::System::Com::CoCreateGuid;
94
95 let mut guid = core::GUID {
96 data1: 0,
97 data2: 0,
98 data3: 0,
99 data4: [0u8; 8],
100 };
101
102 unsafe {
103 CoCreateGuid(&mut guid);
104 }
105
106 let uuid = Uuid::from_fields(guid.data1, guid.data2, guid.data3, &guid.data4);
107
108 assert_eq!(Variant::RFC4122, uuid.get_variant());
109 assert_eq!(Some(Version::Random), uuid.get_version());
110 }
111
112 fn main() {}