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 #[cfg(feature = "with-uuid")]
42 impl Into<Uuid> for CFUUID {
into(self) -> Uuid43     fn into(self) -> Uuid {
44         let b = unsafe {
45             CFUUIDGetUUIDBytes(self.0)
46         };
47         let bytes = [
48             b.byte0,
49             b.byte1,
50             b.byte2,
51             b.byte3,
52             b.byte4,
53             b.byte5,
54             b.byte6,
55             b.byte7,
56             b.byte8,
57             b.byte9,
58             b.byte10,
59             b.byte11,
60             b.byte12,
61             b.byte13,
62             b.byte14,
63             b.byte15,
64         ];
65         Uuid::from_bytes(&bytes).unwrap()
66     }
67 }
68 
69 #[cfg(feature = "with-uuid")]
70 impl From<Uuid> for CFUUID {
from(uuid: Uuid) -> CFUUID71     fn from(uuid: Uuid) -> CFUUID {
72         let b = uuid.as_bytes();
73         let bytes = CFUUIDBytes {
74             byte0: b[0],
75             byte1: b[1],
76             byte2: b[2],
77             byte3: b[3],
78             byte4: b[4],
79             byte5: b[5],
80             byte6: b[6],
81             byte7: b[7],
82             byte8: b[8],
83             byte9: b[9],
84             byte10: b[10],
85             byte11: b[11],
86             byte12: b[12],
87             byte13: b[13],
88             byte14: b[14],
89             byte15: b[15],
90         };
91         unsafe {
92             let uuid_ref = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, bytes);
93             TCFType::wrap_under_create_rule(uuid_ref)
94         }
95     }
96 }
97 
98 
99 #[cfg(test)]
100 #[cfg(feature = "with-uuid")]
101 mod test {
102     use super::CFUUID;
103     use uuid::Uuid;
104 
105     #[test]
uuid_conversion()106     fn uuid_conversion() {
107         let cf_uuid = CFUUID::new();
108         let uuid: Uuid = cf_uuid.clone().into();
109         let converted = CFUUID::from(uuid);
110         assert!(cf_uuid == converted);
111     }
112 }
113