core_foundation/lib.rs
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#![allow(non_snake_case)]
11
12//! This crate provides wrappers around the underlying CoreFoundation
13//! types and functions that are available on Apple's operating systems.
14//!
15//! It also provides a framework for other crates to use when wrapping
16//! other frameworks that use the CoreFoundation framework.
17
18use crate::base::TCFType;
19
20pub unsafe trait ConcreteCFType: TCFType {}
21
22/// Declare a Rust type that wraps an underlying CoreFoundation type.
23///
24/// This will provide an implementation of `Drop` using [`CFRelease`].
25/// The type must have an implementation of the [`TCFType`] trait, usually
26/// provided using the [`impl_TCFType`] macro.
27///
28/// ```
29/// use core_foundation::{declare_TCFType, impl_TCFType};
30/// // Make sure that the `TCFType` trait is in scope.
31/// use core_foundation::base::{CFTypeID, TCFType};
32///
33/// extern "C" {
34/// // We need a function that returns the `CFTypeID`.
35/// pub fn ShrubberyGetTypeID() -> CFTypeID;
36/// }
37///
38/// pub struct __Shrubbery {}
39/// // The ref type must be a pointer to the underlying struct.
40/// pub type ShrubberyRef = *const __Shrubbery;
41///
42/// declare_TCFType!(Shrubbery, ShrubberyRef);
43/// impl_TCFType!(Shrubbery, ShrubberyRef, ShrubberyGetTypeID);
44/// # fn main() {}
45/// ```
46///
47/// [`CFRelease`]: https://developer.apple.com/documentation/corefoundation/1521153-cfrelease
48/// [`TCFType`]: base/trait.TCFType.html
49/// [`impl_TCFType`]: macro.impl_TCFType.html
50#[macro_export]
51macro_rules! declare_TCFType {
52 (
53 $(#[$doc:meta])*
54 $ty:ident, $raw:ident
55 ) => {
56 $(#[$doc])*
57 pub struct $ty($raw);
58
59 impl Drop for $ty {
60 fn drop(&mut self) {
61 unsafe { $crate::base::CFRelease(self.as_CFTypeRef()) }
62 }
63 }
64 }
65}
66
67/// Provide an implementation of the [`TCFType`] trait for the Rust
68/// wrapper type around an underlying CoreFoundation type.
69///
70/// See [`declare_TCFType`] for details.
71///
72/// [`declare_TCFType`]: macro.declare_TCFType.html
73/// [`TCFType`]: base/trait.TCFType.html
74#[macro_export]
75macro_rules! impl_TCFType {
76 ($ty:ident, $ty_ref:ident, $ty_id:ident) => {
77 impl_TCFType!($ty<>, $ty_ref, $ty_id);
78 unsafe impl $crate::ConcreteCFType for $ty { }
79 };
80
81 ($ty:ident<$($p:ident $(: $bound:path)*),*>, $ty_ref:ident, $ty_id:ident) => {
82 impl<$($p $(: $bound)*),*> $crate::base::TCFType for $ty<$($p),*> {
83 type Ref = $ty_ref;
84
85 #[inline]
86 fn as_concrete_TypeRef(&self) -> $ty_ref {
87 self.0
88 }
89
90 #[inline]
91 unsafe fn wrap_under_get_rule(reference: $ty_ref) -> Self {
92 assert!(!reference.is_null(), "Attempted to create a NULL object.");
93 let reference = $crate::base::CFRetain(reference as *const ::std::os::raw::c_void) as $ty_ref;
94 $crate::base::TCFType::wrap_under_create_rule(reference)
95 }
96
97 #[inline]
98 fn as_CFTypeRef(&self) -> $crate::base::CFTypeRef {
99 self.as_concrete_TypeRef() as $crate::base::CFTypeRef
100 }
101
102 #[inline]
103 unsafe fn wrap_under_create_rule(reference: $ty_ref) -> Self {
104 assert!(!reference.is_null(), "Attempted to create a NULL object.");
105 // we need one PhantomData for each type parameter so call ourselves
106 // again with @Phantom $p to produce that
107 $ty(reference $(, impl_TCFType!(@Phantom $p))*)
108 }
109
110 #[inline]
111 fn type_id() -> $crate::base::CFTypeID {
112 unsafe {
113 $ty_id()
114 }
115 }
116 }
117
118 impl Clone for $ty {
119 #[inline]
120 fn clone(&self) -> $ty {
121 unsafe {
122 $ty::wrap_under_get_rule(self.0)
123 }
124 }
125 }
126
127 impl PartialEq for $ty {
128 #[inline]
129 fn eq(&self, other: &$ty) -> bool {
130 self.as_CFType().eq(&other.as_CFType())
131 }
132 }
133
134 impl Eq for $ty { }
135
136 unsafe impl<'a> $crate::base::ToVoid<$ty> for &'a $ty {
137 fn to_void(&self) -> *const ::std::os::raw::c_void {
138 use $crate::base::TCFTypeRef;
139 self.as_concrete_TypeRef().as_void_ptr()
140 }
141 }
142
143 unsafe impl $crate::base::ToVoid<$ty> for $ty {
144 fn to_void(&self) -> *const ::std::os::raw::c_void {
145 use $crate::base::TCFTypeRef;
146 self.as_concrete_TypeRef().as_void_ptr()
147 }
148 }
149
150 unsafe impl $crate::base::ToVoid<$ty> for $ty_ref {
151 fn to_void(&self) -> *const ::std::os::raw::c_void {
152 use $crate::base::TCFTypeRef;
153 self.as_void_ptr()
154 }
155 }
156
157 };
158
159 (@Phantom $x:ident) => { ::std::marker::PhantomData };
160}
161
162/// Implement `std::fmt::Debug` for the given type.
163///
164/// This will invoke the implementation of `Debug` for [`CFType`]
165/// which invokes [`CFCopyDescription`].
166///
167/// The type must have an implementation of the [`TCFType`] trait, usually
168/// provided using the [`impl_TCFType`] macro.
169///
170/// [`CFType`]: base/struct.CFType.html#impl-Debug
171/// [`CFCopyDescription`]: https://developer.apple.com/documentation/corefoundation/1521252-cfcopydescription?language=objc
172/// [`TCFType`]: base/trait.TCFType.html
173/// [`impl_TCFType`]: macro.impl_TCFType.html
174#[macro_export]
175macro_rules! impl_CFTypeDescription {
176 ($ty:ident) => {
177 // it's fine to use an empty <> list
178 impl_CFTypeDescription!($ty<>);
179 };
180 ($ty:ident<$($p:ident $(: $bound:path)*),*>) => {
181 impl<$($p $(: $bound)*),*> ::std::fmt::Debug for $ty<$($p),*> {
182 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
183 self.as_CFType().fmt(f)
184 }
185 }
186 }
187}
188
189#[macro_export]
190macro_rules! impl_CFComparison {
191 ($ty:ident, $compare:ident) => {
192 impl PartialOrd for $ty {
193 #[inline]
194 fn partial_cmp(&self, other: &$ty) -> Option<::std::cmp::Ordering> {
195 unsafe {
196 Some(
197 $compare(
198 self.as_concrete_TypeRef(),
199 other.as_concrete_TypeRef(),
200 ::std::ptr::null_mut(),
201 )
202 .into(),
203 )
204 }
205 }
206 }
207
208 impl Ord for $ty {
209 #[inline]
210 fn cmp(&self, other: &$ty) -> ::std::cmp::Ordering {
211 self.partial_cmp(other).unwrap()
212 }
213 }
214 };
215}
216
217pub mod array;
218pub mod attributed_string;
219pub mod base;
220pub mod boolean;
221pub mod bundle;
222pub mod characterset;
223pub mod data;
224pub mod date;
225pub mod dictionary;
226pub mod error;
227pub mod filedescriptor;
228pub mod mach_port;
229pub mod number;
230pub mod propertylist;
231pub mod runloop;
232pub mod set;
233pub mod string;
234pub mod timezone;
235pub mod url;
236pub mod uuid;