objc2_core_foundation/
base.rs

1// The header `CoreFoundation/CFBase.h` contains:
2//
3// #if defined(__WIN64__) && !defined(__LLP64__)
4// #define __LLP64__ 1
5// #endif
6//
7// #if __LLP64__
8// typedef unsigned long long CFTypeID;
9// typedef unsigned long long CFOptionFlags;
10// typedef unsigned long long CFHashCode;
11// typedef signed long long CFIndex;
12// #else
13// typedef unsigned long CFTypeID;
14// typedef unsigned long CFOptionFlags;
15// typedef unsigned long CFHashCode;
16// typedef signed long CFIndex;
17// #endif
18//
19// Looking at the corresponding Rust definitions for longs:
20// <https://doc.rust-lang.org/1.83.0/src/core/ffi/mod.rs.html#168-179>
21// cfg_if! {
22//     if #[cfg(all(target_pointer_width = "64", not(windows)))] {
23//         pub type c_long = i64;
24//         pub type c_ulong = u64;
25//     } else {
26//         // The minimal size of `long` in the C standard is 32 bits
27//         pub type c_long = i32;
28//         pub type c_ulong = u32;
29//     }
30// }
31// <https://doc.rust-lang.org/1.83.0/src/core/ffi/mod.rs.html#65-66>
32// pub type c_longlong = i64;
33// pub type c_ulonglong = u64;
34//
35// It becomes easy to convince ourselves that combined, these amount to making
36// these types be 32-bit on systems with 32-bit pointers and 64-bit on systems
37// with 64-bit pointers.
38//
39// That means we can use `isize`/`usize`, which is more ergonomic.
40
41use core::cell::UnsafeCell;
42use core::cmp::Ordering;
43use core::convert::AsRef;
44use core::fmt;
45use core::hash;
46use core::marker::{PhantomData, PhantomPinned};
47
48use crate::CFComparisonResult;
49use crate::ConcreteType;
50use crate::{CFEqual, CFHash, Type};
51
52/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cftypeid?language=objc)
53pub type CFTypeID = usize;
54
55/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfoptionflags?language=objc)
56pub type CFOptionFlags = usize;
57
58/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfhashcode?language=objc)
59pub type CFHashCode = usize;
60
61/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfindex?language=objc)
62pub type CFIndex = isize;
63
64// Manually define CFType
65
66/// An instance of a Core Foundation type.
67///
68/// This is meant to be used behind a reference. In the future, this will be
69/// defined as an [`extern type`][RFC-1861].
70///
71/// All Core Foundation types [`Deref`](std::ops::Deref) to this type (it can
72/// be considered the "root" type).
73///
74/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cftype?language=objc).
75///
76/// [RFC-1861]: https://rust-lang.github.io/rfcs/1861-extern-types.html
77#[repr(C)]
78pub struct CFType {
79    inner: [u8; 0],
80    _p: UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>,
81}
82
83impl CFType {
84    /// Attempt to downcast the type to that of type `T`.
85    ///
86    /// This is the reference-variant. Use [`CFRetained::downcast`] if you
87    /// want to convert a retained type. See also [`ConcreteType`] for more
88    /// details on which types support being converted to.
89    ///
90    /// [`CFRetained::downcast`]: crate::CFRetained::downcast
91    //
92    // Not #[inline], we call two functions here.
93    #[doc(alias = "CFGetTypeID")]
94    pub fn downcast_ref<T: ConcreteType>(&self) -> Option<&T> {
95        extern "C-unwind" {
96            fn CFGetTypeID(cf: Option<&CFType>) -> CFTypeID;
97        }
98
99        // SAFETY: The pointer is valid.
100        if unsafe { CFGetTypeID(Some(self)) } == T::type_id() {
101            let ptr: *const Self = self;
102            let ptr: *const T = ptr.cast();
103            // SAFETY: Just checked that the object is a class of type `T`.
104            // Additionally, `ConcreteType::type_id` is guaranteed to uniquely
105            // identify the class (including ruling out mutable subclasses),
106            // so we know for _sure_ that the class is actually of that type
107            // here.
108            let this: &T = unsafe { &*ptr };
109            Some(this)
110        } else {
111            None
112        }
113    }
114}
115
116// Reflexive AsRef impl.
117impl AsRef<Self> for CFType {
118    #[inline]
119    fn as_ref(&self) -> &Self {
120        self
121    }
122}
123
124// SAFETY: CFType represents a CoreFoundation-like type (even though it isn't
125// a real type itself).
126unsafe impl Type for CFType {}
127
128impl fmt::Debug for CFType {
129    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130        #[cfg(feature = "CFString")]
131        {
132            let desc = crate::CFCopyDescription(Some(self)).expect("must have description");
133            write!(f, "{desc}")
134        }
135        #[cfg(not(feature = "CFString"))]
136        {
137            f.debug_struct("<CoreFoundation type (enable CFString feature for more info)>")
138                .finish_non_exhaustive()
139        }
140    }
141}
142
143// Equality in CF has approximately the same semantics as Rust equality.
144//
145// From the docs:
146// > Equality is something specific to each Core Foundation opaque type. For
147// > example, two CFNumber objects are equal if the numeric values they
148// > represent are equal. Two CFString objects are equal if they represent
149// > identical sequences of characters, regardless of encoding.
150impl PartialEq for CFType {
151    #[inline]
152    #[doc(alias = "CFEqual")]
153    fn eq(&self, other: &Self) -> bool {
154        CFEqual(Some(self), Some(other))
155    }
156}
157
158// Similar to NSObject, most types' equality is reflexive.
159impl Eq for CFType {}
160
161// From the documentation for CFHash:
162// > Two objects that are equal (as determined by the `CFEqual` function) have
163// > the same hashing value. However, the converse is not true: two objects
164// > with the same hashing value might not be equal. That is, hashing values
165// > are not necessarily unique.
166//
167// I.e. the same semantics as Rust's `Hash`.
168impl hash::Hash for CFType {
169    #[doc(alias = "CFHash")]
170    fn hash<H: hash::Hasher>(&self, state: &mut H) {
171        CFHash(Some(self)).hash(state);
172    }
173}
174
175crate::__cf_type_objc2!(CFType, crate::__cf_macro_helpers::Encoding::Void);
176
177// NOTE: impl AsRef<CFType> for AnyObject would probably not be valid, since
178// not all Objective-C objects can be used as CoreFoundation objects (?)
179
180impl Default for CFComparisonResult {
181    #[inline]
182    fn default() -> Self {
183        Self::CompareEqualTo
184    }
185}
186
187impl From<Ordering> for CFComparisonResult {
188    #[inline]
189    fn from(order: Ordering) -> Self {
190        match order {
191            Ordering::Less => Self::CompareLessThan,
192            Ordering::Equal => Self::CompareEqualTo,
193            Ordering::Greater => Self::CompareGreaterThan,
194        }
195    }
196}
197
198impl From<CFComparisonResult> for Ordering {
199    #[inline]
200    fn from(comparison_result: CFComparisonResult) -> Self {
201        match comparison_result.0 {
202            ..=-1 => Self::Less,  // ..=CFComparisonResult::CompareLessThan
203            0 => Self::Equal,     // CFComparisonResult::CompareEqualTo
204            1.. => Self::Greater, // CFComparisonResult::CompareGreaterThan..
205            #[allow(unreachable_patterns)] // MSRV between 1.73 and 1.76
206            _ => Self::Equal,
207        }
208    }
209}