objc2/ffi/types.rs
1//! Objective-C type aliases.
2#![allow(non_camel_case_types)]
3
4// # Why isize/usize is correct for NSInteger/NSUInteger
5//
6// ## Apple
7// The documentation clearly states:
8//
9// > When building 32-bit applications, NSInteger is a 32-bit integer. A
10// 64-bit application treats NSInteger as a 64-bit integer.
11//
12// And the header file defines them like so:
13//
14// #if __LP64__ || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
15// typedef long NSInteger;
16// typedef unsigned long NSUInteger;
17// #else
18// typedef int NSInteger;
19// typedef unsigned int NSUInteger;
20// #endif
21//
22// Rust (or at least `libc`) has no targets where c_int/c_uint are not 32-bit,
23// so that part is correct. By manual inspection it is found that the only
24// platform where c_long/c_ulong differs from isize/usize is on Windows.
25// However Apple's libraries are only designed to work on 32-bit Windows, so
26// this case should be fine as well.
27//
28// Likewise for NSUInteger.
29//
30//
31// ## GNUStep / WinObjC
32//
33// Defined as intptr_t/uintptr_t, which is exactly the same as isize/usize.
34//
35//
36// ## ObjFW
37//
38// Doesn't define these, but e.g. -[OFString length] returns size_t, so our
39// definitions should be correct on effectively all targets.
40//
41// Things might change slightly in the future, see
42// <https://internals.rust-lang.org/t/pre-rfc-usize-is-not-size-t/15369>.
43
44/// A signed integer value type.
45///
46/// This is guaranteed to always be a type-alias to [`isize`]. That means it
47/// is valid to use `#[repr(isize)]` on enums and structs with size
48/// `NSInteger`.
49///
50/// See also the [corresponding documentation entry][docs].
51///
52/// [docs]: https://developer.apple.com/documentation/objectivec/nsinteger?language=objc
53///
54///
55/// # Examples
56///
57/// ```
58/// use core::mem::size_of;
59/// use objc2::ffi::NSInteger;
60///
61/// #[repr(isize)]
62/// pub enum NSComparisonResult {
63/// NSOrderedAscending = -1,
64/// NSOrderedSame = 0,
65/// NSOrderedDescending = 1,
66/// }
67///
68/// assert_eq!(size_of::<NSComparisonResult>(), size_of::<NSInteger>());
69/// ```
70pub type NSInteger = isize;
71
72/// Describes an unsigned integer.
73///
74/// This is guaranteed to always be a type-alias to [`usize`]. That means it
75/// is valid to use `#[repr(usize)]` on enums and structs with size
76/// `NSUInteger`.
77///
78/// See also the [corresponding documentation entry][docs].
79///
80/// [docs]: https://developer.apple.com/documentation/objectivec/nsuinteger?language=objc
81///
82///
83/// # Examples
84///
85/// ```
86/// use objc2::ffi::NSUInteger;
87///
88/// extern "C-unwind" {
89/// fn some_external_function() -> NSUInteger;
90/// }
91/// ```
92///
93/// ```
94/// use core::mem::size_of;
95/// use objc2::ffi::NSUInteger;
96///
97/// #[repr(usize)]
98/// enum CLRegionState {
99/// Unknown = 0,
100/// Inside = 1,
101/// Outside = 2,
102/// }
103///
104/// assert_eq!(size_of::<CLRegionState>(), size_of::<NSUInteger>());
105/// ```
106pub type NSUInteger = usize;
107
108/// The maximum value for a [`NSInteger`].
109pub const NSIntegerMax: NSInteger = NSInteger::MAX;
110
111/// The minimum value for a [`NSInteger`].
112pub const NSIntegerMin: NSInteger = NSInteger::MIN;
113
114/// The maximum value for a [`NSUInteger`].
115pub const NSUIntegerMax: NSUInteger = NSUInteger::MAX;