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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#[cfg(feature = "with-uuid")]
extern crate uuid;
pub use core_foundation_sys::uuid::*;
use core_foundation_sys::base::kCFAllocatorDefault;
use base::TCFType;
#[cfg(feature = "with-uuid")]
use self::uuid::Uuid;
declare_TCFType! {
CFUUID, CFUUIDRef
}
impl_TCFType!(CFUUID, CFUUIDRef, CFUUIDGetTypeID);
impl_CFTypeDescription!(CFUUID);
impl CFUUID {
#[inline]
pub fn new() -> CFUUID {
unsafe {
let uuid_ref = CFUUIDCreate(kCFAllocatorDefault);
TCFType::wrap_under_create_rule(uuid_ref)
}
}
}
#[cfg(feature = "with-uuid")]
impl Into<Uuid> for CFUUID {
fn into(self) -> Uuid {
let b = unsafe {
CFUUIDGetUUIDBytes(self.0)
};
let bytes = [
b.byte0,
b.byte1,
b.byte2,
b.byte3,
b.byte4,
b.byte5,
b.byte6,
b.byte7,
b.byte8,
b.byte9,
b.byte10,
b.byte11,
b.byte12,
b.byte13,
b.byte14,
b.byte15,
];
Uuid::from_bytes(&bytes).unwrap()
}
}
#[cfg(feature = "with-uuid")]
impl From<Uuid> for CFUUID {
fn from(uuid: Uuid) -> CFUUID {
let b = uuid.as_bytes();
let bytes = CFUUIDBytes {
byte0: b[0],
byte1: b[1],
byte2: b[2],
byte3: b[3],
byte4: b[4],
byte5: b[5],
byte6: b[6],
byte7: b[7],
byte8: b[8],
byte9: b[9],
byte10: b[10],
byte11: b[11],
byte12: b[12],
byte13: b[13],
byte14: b[14],
byte15: b[15],
};
unsafe {
let uuid_ref = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, bytes);
TCFType::wrap_under_create_rule(uuid_ref)
}
}
}
#[cfg(test)]
#[cfg(feature = "with-uuid")]
mod test {
use super::CFUUID;
use uuid::Uuid;
#[test]
fn uuid_conversion() {
let cf_uuid = CFUUID::new();
let uuid: Uuid = cf_uuid.clone().into();
let converted = CFUUID::from(uuid);
assert!(cf_uuid == converted);
}
}