Skip to content

Commit b4326d0

Browse files
committed
Remove ProtocolType implementation for NSObject
It is more like an odd edge-case that `NSObject` implements `ProtocolType` and can be used as `ProtocolObject<NSObject>`, users should instead use `NSObjectProtocol` which works like any other protocol.
1 parent 7c745c2 commit b4326d0

File tree

6 files changed

+23
-39
lines changed

6 files changed

+23
-39
lines changed

crates/objc2/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4646
### Fixed
4747
* Fixed the name of the protocol that `NSObjectProtocol` references.
4848

49+
### Removed
50+
* **BREAKING**: Removed `ProtocolType` implementation for `NSObject`.
51+
Use the more precise `NSObjectProtocol` trait instead!
52+
4953

5054
## 0.4.1 - 2023-07-31
5155

crates/objc2/src/protocol_type.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ use crate::runtime::AnyProtocol;
2323
///
2424
/// ```
2525
/// use objc2::ProtocolType;
26-
/// use objc2::runtime::NSObject;
27-
/// // Get a protocol object representing `NSObject`
28-
/// let protocol = NSObject::protocol().expect("NSObject to have a protocol");
29-
/// assert_eq!(NSObject::NAME, protocol.name());
26+
/// use objc2::runtime::NSObjectProtocol;
27+
/// // Get a protocol object representing the `NSObject` protocol
28+
/// let protocol = <dyn NSObjectProtocol>::protocol().expect("NSObject to have a protocol");
29+
/// assert_eq!(<dyn NSObjectProtocol>::NAME, protocol.name());
3030
/// ```
3131
///
3232
/// Use the [`extern_protocol!`] macro to implement this trait for a type.

crates/objc2/src/runtime/nsobject.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::hash;
33

44
use crate::mutability::Root;
55
use crate::rc::{DefaultId, Id};
6-
use crate::runtime::{AnyClass, AnyObject, AnyProtocol, ImplementedBy, ProtocolObject};
6+
use crate::runtime::{AnyClass, AnyObject, ProtocolObject};
77
use crate::{extern_methods, msg_send, msg_send_id, Message};
88
use crate::{ClassType, ProtocolType};
99

@@ -170,26 +170,6 @@ crate::__inner_extern_protocol!(
170170

171171
unsafe impl NSObjectProtocol for NSObject {}
172172

173-
unsafe impl ProtocolType for NSObject {
174-
const NAME: &'static str = "NSObject";
175-
176-
fn protocol() -> Option<&'static AnyProtocol> {
177-
Some(
178-
AnyProtocol::get(<Self as ProtocolType>::NAME)
179-
.expect("could not find NSObject protocol"),
180-
)
181-
}
182-
183-
const __INNER: () = ();
184-
}
185-
186-
unsafe impl<T> ImplementedBy<T> for NSObject
187-
where
188-
T: ?Sized + Message + NSObjectProtocol,
189-
{
190-
const __INNER: () = ();
191-
}
192-
193173
extern_methods!(
194174
unsafe impl NSObject {
195175
/// Create a new empty `NSObject`.
@@ -234,7 +214,7 @@ impl fmt::Debug for NSObject {
234214
#[doc(alias = "description")]
235215
#[doc(alias = "debugDescription")]
236216
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237-
let obj: &ProtocolObject<NSObject> = ProtocolObject::from_ref(self);
217+
let obj: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(self);
238218
obj.fmt(f)
239219
}
240220
}

crates/objc2/src/runtime/nsproxy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::fmt;
22
use core::hash;
33

44
use crate::mutability::Root;
5-
use crate::runtime::{AnyClass, AnyObject, NSObject, NSObjectProtocol, ProtocolObject};
5+
use crate::runtime::{AnyClass, AnyObject, NSObjectProtocol, ProtocolObject};
66
use crate::ClassType;
77

88
crate::__emit_struct! {
@@ -97,7 +97,7 @@ impl fmt::Debug for NSProxy {
9797
#[doc(alias = "description")]
9898
#[doc(alias = "debugDescription")]
9999
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100-
let obj: &ProtocolObject<NSObject> = ProtocolObject::from_ref(self);
100+
let obj: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(self);
101101
obj.fmt(f)
102102
}
103103
}

crates/objc2/src/runtime/protocol_object.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,39 +272,39 @@ mod tests {
272272
#[test]
273273
fn impl_traits() {
274274
assert_impl_all!(NSObject: NSObjectProtocol);
275-
assert_impl_all!(ProtocolObject<NSObject>: NSObjectProtocol);
275+
assert_impl_all!(ProtocolObject<dyn NSObjectProtocol>: NSObjectProtocol);
276276
assert_not_impl_any!(ProtocolObject<dyn Foo>: NSObjectProtocol);
277277
assert_impl_all!(ProtocolObject<dyn Bar>: NSObjectProtocol);
278278
assert_impl_all!(ProtocolObject<dyn FooBar>: NSObjectProtocol);
279279
assert_impl_all!(ProtocolObject<dyn FooFooBar>: NSObjectProtocol);
280280
assert_impl_all!(DummyClass: NSObjectProtocol);
281281

282282
assert_not_impl_any!(NSObject: Foo);
283-
assert_not_impl_any!(ProtocolObject<NSObject>: Foo);
283+
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: Foo);
284284
assert_impl_all!(ProtocolObject<dyn Foo>: Foo);
285285
assert_not_impl_any!(ProtocolObject<dyn Bar>: Foo);
286286
assert_impl_all!(ProtocolObject<dyn FooBar>: Foo);
287287
assert_impl_all!(ProtocolObject<dyn FooFooBar>: Foo);
288288
assert_impl_all!(DummyClass: Foo);
289289

290290
assert_not_impl_any!(NSObject: Bar);
291-
assert_not_impl_any!(ProtocolObject<NSObject>: Bar);
291+
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: Bar);
292292
assert_not_impl_any!(ProtocolObject<dyn Foo>: Bar);
293293
assert_impl_all!(ProtocolObject<dyn Bar>: Bar);
294294
assert_impl_all!(ProtocolObject<dyn FooBar>: Bar);
295295
assert_impl_all!(ProtocolObject<dyn FooFooBar>: Bar);
296296
assert_impl_all!(DummyClass: Bar);
297297

298298
assert_not_impl_any!(NSObject: FooBar);
299-
assert_not_impl_any!(ProtocolObject<NSObject>: FooBar);
299+
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: FooBar);
300300
assert_not_impl_any!(ProtocolObject<dyn Foo>: FooBar);
301301
assert_not_impl_any!(ProtocolObject<dyn Bar>: FooBar);
302302
assert_impl_all!(ProtocolObject<dyn FooBar>: FooBar);
303303
assert_impl_all!(ProtocolObject<dyn FooFooBar>: FooBar);
304304
assert_impl_all!(DummyClass: FooBar);
305305

306306
assert_not_impl_any!(NSObject: FooFooBar);
307-
assert_not_impl_any!(ProtocolObject<NSObject>: FooFooBar);
307+
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: FooFooBar);
308308
assert_not_impl_any!(ProtocolObject<dyn Foo>: FooFooBar);
309309
assert_not_impl_any!(ProtocolObject<dyn Bar>: FooFooBar);
310310
assert_not_impl_any!(ProtocolObject<dyn FooBar>: FooFooBar);
@@ -326,10 +326,10 @@ mod tests {
326326
let foo: &ProtocolObject<dyn Foo> = ProtocolObject::from_ref(&*obj);
327327
let _foo: &ProtocolObject<dyn Foo> = ProtocolObject::from_ref(foo);
328328

329-
let _nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(foobar);
330-
let _nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(bar);
331-
let nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(&*obj);
332-
let _nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(nsobject);
329+
let _nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(foobar);
330+
let _nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(bar);
331+
let nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(&*obj);
332+
let _nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(nsobject);
333333

334334
let _foobar: &mut ProtocolObject<dyn FooBar> = ProtocolObject::from_mut(&mut *obj);
335335
let _foobar: Id<ProtocolObject<dyn FooBar>> = ProtocolObject::from_id(obj);

crates/tests/src/test_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,6 @@ fn test_protocol() {
326326
#[cfg(feature = "Foundation_all")]
327327
assert_eq!(MyTestObject::h().as_i32(), 8);
328328

329-
// Check that transforming to `NSObject` works
330-
let _obj: &ProtocolObject<NSObject> = ProtocolObject::from_ref(&*proto);
329+
// Check that transforming to `NSObjectProtocol` works
330+
let _obj: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(&*proto);
331331
}

0 commit comments

Comments
 (0)