1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use super::{sys, CFType, CFTypeID};
use std::{cmp::Ordering, fmt};
subclass! {
/// A boolean object.
///
/// Documentation:
/// [Swift](https://developer.apple.com/documentation/corefoundation/cfboolean?language=swift) |
/// [Objective-C](https://developer.apple.com/documentation/corefoundation/cfboolean?language=objc)
#[derive(PartialEq, Hash)]
pub class CFBoolean: CFType<'static>;
}
// `CFBoolean` is bridged to `NSNumber` but not the other way around.
#[cfg(feature = "foundation")]
mod foundation_casts {
use super::*;
use crate::{core::Arc, foundation::NSNumber};
impl From<Arc<CFBoolean>> for Arc<NSNumber> {
#[inline]
fn from(boolean: Arc<CFBoolean>) -> Self {
unsafe { Arc::cast_unchecked(boolean) }
}
}
impl AsRef<NSNumber> for CFBoolean {
#[inline]
fn as_ref(&self) -> &NSNumber {
unsafe { &*(self as *const Self as *const NSNumber) }
}
}
}
impl fmt::Debug for CFBoolean {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_bool().fmt(f)
}
}
impl fmt::Display for CFBoolean {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_bool().fmt(f)
}
}
impl Eq for CFBoolean {}
impl PartialOrd for CFBoolean {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CFBoolean {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.as_bool().cmp(&other.as_bool())
}
}
impl Default for &CFBoolean {
#[inline]
fn default() -> Self {
CFBoolean::false_value()
}
}
impl CFBoolean {
/// Returns a reference to a `false` value.
///
/// This internally references
/// [`kCFBooleanFalse`](https://developer.apple.com/documentation/corefoundation/kCFBooleanFalse).
#[inline]
#[doc(alias = "kCFBooleanFalse")]
pub fn false_value() -> &'static Self {
extern "C" {
static kCFBooleanFalse: &'static CFBoolean;
}
unsafe { kCFBooleanFalse }
}
/// Returns a reference to a `true` value.
///
/// This internally references
/// [`kCFBooleanTrue`](https://developer.apple.com/documentation/corefoundation/kCFBooleanTrue).
#[inline]
#[doc(alias = "kCFBooleanTrue")]
pub fn true_value() -> &'static Self {
extern "C" {
static kCFBooleanTrue: &'static CFBoolean;
}
unsafe { kCFBooleanTrue }
}
}
impl CFBoolean {
/// Returns the type identifier for `CFBoolean`.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1541762-cfbooleangettypeid?language=objc).
#[inline]
#[doc(alias = "CFBooleanGetTypeID")]
pub fn type_id() -> CFTypeID {
unsafe { sys::CFBooleanGetTypeID() }
}
/// Returns `kCFBooleanFalse` if `value` is `false`, or `kCFBooleanTrue`
/// if `value` is `true`.
#[inline]
pub fn new(value: bool) -> &'static CFBoolean {
if value {
Self::true_value()
} else {
Self::false_value()
}
}
/// Returns that native `bool` value.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1541447-cfbooleangetvalue?language=objc).
#[inline]
#[doc(alias = "CFBooleanGetValue")]
pub fn as_bool(&self) -> bool {
unsafe { sys::CFBooleanGetValue(self) != 0 }
}
}