Skip to content

Commit b1179f7

Browse files
authored
Merge pull request #188 from madsmtm/extern-class-macro
`extern_class!` macro
2 parents 5d968b6 + c4a660f commit b1179f7

17 files changed

+247
-120
lines changed

objc2-foundation/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1010
* Added `MainThreadMarker` to help with designing APIs where a method is only
1111
safe to call on the main thread.
1212
* Added `NSException` object.
13+
* Added `extern_class!` macro to help with defining other classes.
14+
* Expose the `objc2` version that this uses in the crate root.
15+
16+
### Changed
17+
* Changed a few `Debug` impls.
1318

1419

1520
## 0.2.0-alpha.5 - 2022-06-13

objc2-foundation/src/array.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::{
1414
NSRange,
1515
};
1616

17-
object! {
17+
__inner_extern_class! {
1818
/// TODO
1919
///
2020
/// You can have a `Id<NSArray<T, Owned>, Owned>`, which allows mutable access
@@ -25,6 +25,9 @@ object! {
2525
/// TODO: Can we make it impossible? Should we?
2626
///
2727
/// What about `Id<NSArray<T, Shared>, Owned>`?
28+
// `T: PartialEq` bound correct because `NSArray` does deep (instead of
29+
// shallow) equality comparisons.
30+
#[derive(Debug, PartialEq, Eq, Hash)]
2831
unsafe pub struct NSArray<T, O: Ownership>: NSObject {
2932
item: PhantomData<Id<T, O>>,
3033
}
@@ -40,9 +43,10 @@ unsafe impl<T: Sync + Send> Send for NSArray<T, Shared> {}
4043
unsafe impl<T: Sync> Sync for NSArray<T, Owned> {}
4144
unsafe impl<T: Send> Send for NSArray<T, Owned> {}
4245

43-
object! {
46+
__inner_extern_class! {
4447
// TODO: Ensure that this deref to NSArray is safe!
4548
// This "inherits" NSArray, and has the same `Send`/`Sync` impls as that.
49+
#[derive(Debug, PartialEq, Eq, Hash)]
4650
unsafe pub struct NSMutableArray<T, O: Ownership>: NSArray<T, O>, NSObject {}
4751
}
4852

objc2-foundation/src/attributed_string.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
NSCopying, NSDictionary, NSMutableAttributedString, NSMutableCopying, NSObject, NSString,
77
};
88

9-
object! {
9+
extern_class! {
1010
/// A string that has associated attributes for portions of its text.
1111
///
1212
/// Examples of attributes could be: Visual style, hyperlinks, or
@@ -19,6 +19,7 @@ object! {
1919
/// framework contains most of the extension methods.
2020
///
2121
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsattributedstring?language=objc).
22+
#[derive(Debug, PartialEq, Eq, Hash)]
2223
unsafe pub struct NSAttributedString: NSObject;
2324
}
2425

objc2-foundation/src/data.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,29 @@ use objc2::{msg_send, msg_send_id};
1111

1212
use super::{NSCopying, NSMutableCopying, NSObject, NSRange};
1313

14-
object! {
14+
extern_class! {
1515
/// A static byte buffer in memory.
1616
///
1717
/// This is similar to a [`slice`][`prim@slice`] of [`u8`].
1818
///
1919
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsdata?language=objc).
20+
#[derive(Debug, PartialEq, Eq, Hash)]
2021
unsafe pub struct NSData: NSObject;
2122
}
2223

2324
// TODO: SAFETY
2425
unsafe impl Sync for NSData {}
2526
unsafe impl Send for NSData {}
2627

27-
object! {
28+
extern_class! {
2829
/// A dynamic byte buffer in memory.
2930
///
3031
/// This is the Objective-C equivalent of a [`Vec`] containing [`u8`].
3132
///
3233
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsmutabledata?language=objc).
3334
///
3435
/// [`Vec`]: std::vec::Vec
36+
#[derive(Debug, PartialEq, Eq, Hash)]
3537
unsafe pub struct NSMutableData: NSData, NSObject;
3638
}
3739

objc2-foundation/src/dictionary.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use objc2::{msg_send, msg_send_id, Message};
99

1010
use super::{NSArray, NSCopying, NSEnumerator, NSFastEnumeration, NSObject};
1111

12-
object! {
12+
__inner_extern_class! {
13+
#[derive(Debug, PartialEq, Eq, Hash)]
1314
unsafe pub struct NSDictionary<K, V>: NSObject {
1415
key: PhantomData<Id<K, Shared>>,
1516
obj: PhantomData<Id<V, Owned>>,

objc2-foundation/src/exception.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::fmt;
12
use core::hint::unreachable_unchecked;
23
use core::panic::{RefUnwindSafe, UnwindSafe};
34

@@ -8,7 +9,7 @@ use objc2::{msg_send, msg_send_id, sel};
89

910
use crate::{NSCopying, NSDictionary, NSObject, NSString};
1011

11-
object! {
12+
extern_class! {
1213
/// A special condition that interrupts the normal flow of program
1314
/// execution.
1415
///
@@ -18,6 +19,7 @@ object! {
1819
/// See also [Apple's documentation][doc].
1920
///
2021
/// [doc]: https://developer.apple.com/documentation/foundation/nsexception?language=objc
22+
#[derive(PartialEq, Eq, Hash)]
2123
unsafe pub struct NSException: NSObject;
2224
}
2325

@@ -120,7 +122,18 @@ impl alloc::borrow::ToOwned for NSException {
120122
}
121123
}
122124

123-
// TODO: Better Debug impl
125+
impl fmt::Debug for NSException {
126+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127+
let obj: &Object = self.as_ref();
128+
write!(f, "{:?} '{}'", obj, self.name())?;
129+
if let Some(reason) = self.reason() {
130+
write!(f, " reason:{}", reason)?;
131+
} else {
132+
write!(f, " reason:(NULL)")?;
133+
}
134+
Ok(())
135+
}
136+
}
124137

125138
#[cfg(test)]
126139
mod tests {
@@ -148,15 +161,13 @@ mod tests {
148161
};
149162

150163
assert_eq!(exc.description(), NSString::from_str(&description));
151-
assert_eq!(format!("{:?}", exc), format!("\"{}\"", description));
164+
165+
let debug = format!("<NSException: {:p}> 'abc' reason:def", exc);
166+
assert_eq!(format!("{:?}", exc), debug);
152167
}
153168

154169
#[test]
155-
#[cfg_attr(
156-
feature = "apple",
157-
should_panic = "called `Result::unwrap()` on an `Err` value: \"def\""
158-
)]
159-
#[cfg_attr(feature = "gnustep-1-7", should_panic = "> NAME:abc REASON:def")]
170+
#[should_panic = "'abc' reason:def"]
160171
fn unwrap() {
161172
let exc = NSException::new(
162173
&NSString::from_str("abc"),

objc2-foundation/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ pub use self::value::NSValue;
7171
#[doc(no_inline)]
7272
pub use objc2::ffi::{NSInteger, NSUInteger};
7373

74+
#[doc(hidden)]
75+
pub use core as __core;
76+
77+
// Expose the version of objc2 that this crate uses
78+
pub use objc2;
79+
7480
#[cfg(feature = "apple")]
7581
#[link(name = "Foundation", kind = "framework")]
7682
extern "C" {}

0 commit comments

Comments
 (0)