Skip to content

Commit

Permalink
Improve definition of CFBase types
Browse files Browse the repository at this point in the history
Specifically CFTypeID, CFOptionFlags, CFHashCode and CFIndex, these can
(and should) all be isize/usize instead of c_long/c_ulong.

Part of #556.
  • Loading branch information
madsmtm committed Jan 6, 2025
1 parent bcd0b83 commit 5bc094b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
51 changes: 51 additions & 0 deletions framework-crates/objc2-core-foundation/src/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// The header `CoreFoundation/CFBase.h` contains:
//
// #if defined(__WIN64__) && !defined(__LLP64__)
// #define __LLP64__ 1
// #endif
//
// #if __LLP64__
// typedef unsigned long long CFTypeID;
// typedef unsigned long long CFOptionFlags;
// typedef unsigned long long CFHashCode;
// typedef signed long long CFIndex;
// #else
// typedef unsigned long CFTypeID;
// typedef unsigned long CFOptionFlags;
// typedef unsigned long CFHashCode;
// typedef signed long CFIndex;
// #endif
//
// Looking at the corresponding Rust definitions for longs:
// <https://doc.rust-lang.org/1.83.0/src/core/ffi/mod.rs.html#168-179>
// cfg_if! {
// if #[cfg(all(target_pointer_width = "64", not(windows)))] {
// pub type c_long = i64;
// pub type c_ulong = u64;
// } else {
// // The minimal size of `long` in the C standard is 32 bits
// pub type c_long = i32;
// pub type c_ulong = u32;
// }
// }
// <https://doc.rust-lang.org/1.83.0/src/core/ffi/mod.rs.html#65-66>
// pub type c_longlong = i64;
// pub type c_ulonglong = u64;
//
// It becomes easy to convince ourselves that combined, these amount to making
// these types be 32-bit on systems with 32-bit pointers and 64-bit on systems
// with 64-bit pointers.
//
// That means we can use `isize`/`usize`, which is more ergonomic.

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cftypeid?language=objc)
pub type CFTypeID = usize;

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfoptionflags?language=objc)
pub type CFOptionFlags = usize;

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfhashcode?language=objc)
pub type CFHashCode = usize;

/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfindex?language=objc)
pub type CFIndex = isize;
4 changes: 4 additions & 0 deletions framework-crates/objc2-core-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "CFBase")]
mod base;
#[cfg(feature = "CFBundle")]
mod bundle;
mod generated;
#[cfg(feature = "CFCGTypes")]
mod geometry;

#[cfg(feature = "CFBase")]
pub use self::base::*;
#[cfg(feature = "CFBundle")]
pub use self::bundle::CFBundleRefNum;
#[allow(unused_imports, unreachable_pub)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ fn.__CFStringMakeConstantString.skipped = true

# Differs based on architecture
typedef.CFBundleRefNum.skipped = true
typedef.CFTypeID.skipped = true
typedef.CFOptionFlags.skipped = true
typedef.CFHashCode.skipped = true
typedef.CFIndex.skipped = true
2 changes: 1 addition & 1 deletion generated

0 comments on commit 5bc094b

Please sign in to comment.