-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathguid.rs
74 lines (61 loc) · 2.14 KB
/
guid.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extern crate uuid;
use std::{fmt, cmp, mem};
use w::shared::guiddef::GUID;
/// Represents a GUID type in the Windows Runtime type system.
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Guid { // TODO: fields should not be public (requires const fn constructor)
pub Data1: u32,
pub Data2: u16,
pub Data3: u16,
pub Data4: [u8; 8]
}
impl AsRef<GUID> for Guid {
#[inline]
fn as_ref(&self) -> &GUID {
unsafe { mem::transmute(self) }
}
}
impl From<GUID> for Guid {
#[inline]
fn from(guid: GUID) -> Self {
unsafe { mem::transmute(guid) }
}
}
impl fmt::Debug for Guid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
self.Data1, self.Data2, self.Data3,
self.Data4[0], self.Data4[1], self.Data4[2], self.Data4[3],
self.Data4[4], self.Data4[5], self.Data4[6], self.Data4[7])
}
}
impl cmp::PartialEq<Guid> for Guid {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.Data1 == other.Data1 && self.Data2 == other.Data2 && self.Data3 == other.Data3 && self.Data4 == other.Data4
}
}
impl cmp::Eq for Guid {}
pub(crate) fn format_for_iid_descriptor(buffer: &mut String, guid: &Guid) { // should be const fn
buffer.push_str(&format!("{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]));
}
pub(crate) fn iid_from_descriptor(descriptor: &str) -> Guid {
use self::uuid::Uuid;
let namespace = Uuid::parse_str("11F47AD5-7B73-42C0-ABAE-878B1E16ADEE").expect("invalid IID namespace UUID");
let iid = Uuid::new_v5(&namespace, descriptor.as_bytes());
let (p1, p2, p3, p4) = iid.as_fields();
Guid { Data1: p1, Data2: p2, Data3: p3, Data4: *p4 }
}
#[cfg(test)]
mod tests {
extern crate test;
#[test]
fn check_size() {
use ::std::mem::size_of;
assert_eq!(size_of::<::Guid>(), size_of::<::w::shared::guiddef::GUID>());
}
}