| 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 |
use core_foundation_sys::base::kCFAllocatorDefault; |
| 13 |
pub use core_foundation_sys::uuid::*; |
| 14 |
|
| 15 |
use crate::base::TCFType; |
| 16 |
|
| 17 |
#[cfg(feature = "with-uuid")] |
| 18 |
use uuid::Uuid; |
| 19 |
|
| 20 |
declare_TCFType! { |
| 21 |
/// A UUID. |
| 22 |
CFUUID, CFUUIDRef |
| 23 |
} |
| 24 |
impl_TCFType!(CFUUID, CFUUIDRef, CFUUIDGetTypeID); |
| 25 |
impl_CFTypeDescription!(CFUUID); |
| 26 |
|
| 27 |
impl 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 |
|
| 37 |
impl Default for CFUUID { |
| 38 |
fn default() -> Self { |
| 39 |
Self::new() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
#[cfg(feature = "with-uuid")] |
| 44 |
impl 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")] |
| 56 |
impl 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")] |
| 86 |
mod 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 |
} |