1use core_foundation_sys::base::kCFAllocatorDefault;
13pub use core_foundation_sys::uuid::*;
14
15use crate::base::TCFType;
16
17#[cfg(feature = "with-uuid")]
18use uuid::Uuid;
19
20declare_TCFType! {
21 CFUUID, CFUUIDRef
23}
24impl_TCFType!(CFUUID, CFUUIDRef, CFUUIDGetTypeID);
25impl_CFTypeDescription!(CFUUID);
26
27impl CFUUID {
28 #[inline]
29 pub fn new() -> CFUUID {
30 unsafe {
31 let uuid_ref = CFUUIDCreate(kCFAllocatorDefault);
32 TCFType::wrap_under_create_rule(uuid_ref)
33 }
34 }
35}
36
37impl Default for CFUUID {
38 fn default() -> Self {
39 Self::new()
40 }
41}
42
43#[cfg(feature = "with-uuid")]
44impl From<CFUUID> for Uuid {
45 fn from(val: CFUUID) -> Self {
46 let b = unsafe { CFUUIDGetUUIDBytes(val.0) };
47 let bytes = [
48 b.byte0, b.byte1, b.byte2, b.byte3, b.byte4, b.byte5, b.byte6, b.byte7, b.byte8,
49 b.byte9, b.byte10, b.byte11, b.byte12, b.byte13, b.byte14, b.byte15,
50 ];
51 Uuid::from_bytes(bytes)
52 }
53}
54
55#[cfg(feature = "with-uuid")]
56impl From<Uuid> for CFUUID {
57 fn from(uuid: Uuid) -> CFUUID {
58 let b = uuid.as_bytes();
59 let bytes = CFUUIDBytes {
60 byte0: b[0],
61 byte1: b[1],
62 byte2: b[2],
63 byte3: b[3],
64 byte4: b[4],
65 byte5: b[5],
66 byte6: b[6],
67 byte7: b[7],
68 byte8: b[8],
69 byte9: b[9],
70 byte10: b[10],
71 byte11: b[11],
72 byte12: b[12],
73 byte13: b[13],
74 byte14: b[14],
75 byte15: b[15],
76 };
77 unsafe {
78 let uuid_ref = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, bytes);
79 TCFType::wrap_under_create_rule(uuid_ref)
80 }
81 }
82}
83
84#[cfg(test)]
85#[cfg(feature = "with-uuid")]
86mod test {
87 use super::CFUUID;
88 use uuid::Uuid;
89
90 #[test]
91 fn uuid_conversion() {
92 let cf_uuid = CFUUID::new();
93 let uuid: Uuid = cf_uuid.clone().into();
94 let converted = CFUUID::from(uuid);
95 assert_eq!(cf_uuid, converted);
96 }
97}