1 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9 
10 //! Core Foundation UUID objects.
11 
12 #[cfg(feature = "with-uuid")]
13 extern crate uuid;
14 
15 pub use core_foundation_sys::uuid::*;
16 use core_foundation_sys::base::kCFAllocatorDefault;
17 
18 use base::TCFType;
19 
20 #[cfg(feature = "with-uuid")]
21 use self::uuid::Uuid;
22 
23 
24 declare_TCFType! {
25     /// A UUID.
26     CFUUID, CFUUIDRef
27 }
28 impl_TCFType!(CFUUID, CFUUIDRef, CFUUIDGetTypeID);
29 impl_CFTypeDescription!(CFUUID);
30 
31 impl CFUUID {
32     #[inline]
new() -> CFUUID33     pub fn new() -> CFUUID {
34         unsafe {
35             let uuid_ref = CFUUIDCreate(kCFAllocatorDefault);
36             TCFType::wrap_under_create_rule(uuid_ref)
37         }
38     }
39 }
40 
41 impl Default for CFUUID {
default() -> Self42     fn default() -> Self {
43         Self::new()
44     }
45 }
46 
47 #[cfg(feature = "with-uuid")]
48 impl Into<Uuid> for CFUUID {
into(self) -> Uuid49     fn into(self) -> Uuid {
50         let b = unsafe {
51             CFUUIDGetUUIDBytes(self.0)
52         };
53         let bytes = [
54             b.byte0,
55             b.byte1,
56             b.byte2,
57             b.byte3,
58             b.byte4,
59             b.byte5,
60             b.byte6,
61             b.byte7,
62             b.byte8,
63             b.byte9,
64             b.byte10,
65             b.byte11,
66             b.byte12,
67             b.byte13,
68             b.byte14,
69             b.byte15,
70         ];
71         Uuid::from_bytes(&bytes).unwrap()
72     }
73 }
74 
75 #[cfg(feature = "with-uuid")]
76 impl From<Uuid> for CFUUID {
from(uuid: Uuid) -> CFUUID77     fn from(uuid: Uuid) -> CFUUID {
78         let b = uuid.as_bytes();
79         let bytes = CFUUIDBytes {
80             byte0: b[0],
81             byte1: b[1],
82             byte2: b[2],
83             byte3: b[3],
84             byte4: b[4],
85             byte5: b[5],
86             byte6: b[6],
87             byte7: b[7],
88             byte8: b[8],
89             byte9: b[9],
90             byte10: b[10],
91             byte11: b[11],
92             byte12: b[12],
93             byte13: b[13],
94             byte14: b[14],
95             byte15: b[15],
96         };
97         unsafe {
98             let uuid_ref = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, bytes);
99             TCFType::wrap_under_create_rule(uuid_ref)
100         }
101     }
102 }
103 
104 
105 #[cfg(test)]
106 #[cfg(feature = "with-uuid")]
107 mod test {
108     use super::CFUUID;
109     use uuid::Uuid;
110 
111     #[test]
uuid_conversion()112     fn uuid_conversion() {
113         let cf_uuid = CFUUID::new();
114         let uuid: Uuid = cf_uuid.clone().into();
115         let converted = CFUUID::from(uuid);
116         assert_eq!(cf_uuid, converted);
117     }
118 }
119