| 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 |
//! A Boolean type. |
| 11 |
|
| 12 |
pub use core_foundation_sys::number::{ |
| 13 |
kCFBooleanFalse, kCFBooleanTrue, CFBooleanGetTypeID, CFBooleanRef, |
| 14 |
}; |
| 15 |
|
| 16 |
use crate::base::TCFType; |
| 17 |
|
| 18 |
declare_TCFType! { |
| 19 |
/// A Boolean type. |
| 20 |
/// |
| 21 |
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. |
| 22 |
CFBoolean, CFBooleanRef |
| 23 |
} |
| 24 |
impl_TCFType!(CFBoolean, CFBooleanRef, CFBooleanGetTypeID); |
| 25 |
impl_CFTypeDescription!(CFBoolean); |
| 26 |
|
| 27 |
impl CFBoolean { |
| 28 |
pub fn true_value() -> CFBoolean { |
| 29 |
unsafe { TCFType::wrap_under_get_rule(kCFBooleanTrue) } |
| 30 |
} |
| 31 |
|
| 32 |
pub fn false_value() -> CFBoolean { |
| 33 |
unsafe { TCFType::wrap_under_get_rule(kCFBooleanFalse) } |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl From<bool> for CFBoolean { |
| 38 |
fn from(value: bool) -> CFBoolean { |
| 39 |
if value { |
| 40 |
CFBoolean::true_value() |
| 41 |
} else { |
| 42 |
CFBoolean::false_value() |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl From<CFBoolean> for bool { |
| 48 |
fn from(value: CFBoolean) -> bool { |
| 49 |
value.0 == unsafe { kCFBooleanTrue } |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
#[cfg(test)] |
| 54 |
mod tests { |
| 55 |
use super::*; |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn to_and_from_bool() { |
| 59 |
let b_false = CFBoolean::from(false); |
| 60 |
let b_true = CFBoolean::from(true); |
| 61 |
assert_ne!(b_false, b_true); |
| 62 |
assert_eq!(b_false, CFBoolean::false_value()); |
| 63 |
assert_eq!(b_true, CFBoolean::true_value()); |
| 64 |
assert!(!bool::from(b_false)); |
| 65 |
assert!(bool::from(b_true)); |
| 66 |
} |
| 67 |
} |