Skip to content

Commit

Permalink
Remove ProtocolType implementation for NSObject
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
madsmtm committed Sep 10, 2023
1 parent 379955d commit 864365e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 39 deletions.
4 changes: 4 additions & 0 deletions crates/objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
* Fixed the name of the protocol that `NSObjectProtocol` references.

### Removed
* **BREAKING**: Removed `ProtocolType` implementation for `NSObject`.
Use the more precise `NSObjectProtocol` trait instead!


## 0.4.1 - 2023-07-31

Expand Down
8 changes: 4 additions & 4 deletions crates/objc2/src/protocol_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use crate::runtime::AnyProtocol;
///
/// ```
/// use objc2::ProtocolType;
/// use objc2::runtime::NSObject;
/// // Get a protocol object representing `NSObject`
/// let protocol = NSObject::protocol().expect("NSObject to have a protocol");
/// assert_eq!(NSObject::NAME, protocol.name());
/// use objc2::runtime::NSObjectProtocol;
/// // Get a protocol object representing the `NSObject` protocol
/// let protocol = <dyn NSObjectProtocol>::protocol().expect("NSObject to have a protocol");
/// assert_eq!(<dyn NSObjectProtocol>::NAME, protocol.name());
/// ```
///
/// Use the [`extern_protocol!`] macro to implement this trait for a type.
Expand Down
24 changes: 2 additions & 22 deletions crates/objc2/src/runtime/nsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::hash;

use crate::mutability::Root;
use crate::rc::{DefaultId, Id};
use crate::runtime::{AnyClass, AnyObject, AnyProtocol, ImplementedBy, ProtocolObject};
use crate::runtime::{AnyClass, AnyObject, ProtocolObject};
use crate::{extern_methods, msg_send, msg_send_id, Message};
use crate::{ClassType, ProtocolType};

Expand Down Expand Up @@ -170,26 +170,6 @@ crate::__inner_extern_protocol!(

unsafe impl NSObjectProtocol for NSObject {}

unsafe impl ProtocolType for NSObject {
const NAME: &'static str = "NSObject";

fn protocol() -> Option<&'static AnyProtocol> {
Some(
AnyProtocol::get(<Self as ProtocolType>::NAME)
.expect("could not find NSObject protocol"),
)
}

const __INNER: () = ();
}

unsafe impl<T> ImplementedBy<T> for NSObject
where
T: ?Sized + Message + NSObjectProtocol,
{
const __INNER: () = ();
}

extern_methods!(
unsafe impl NSObject {
/// Create a new empty `NSObject`.
Expand Down Expand Up @@ -234,7 +214,7 @@ impl fmt::Debug for NSObject {
#[doc(alias = "description")]
#[doc(alias = "debugDescription")]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let obj: &ProtocolObject<NSObject> = ProtocolObject::from_ref(self);
let obj: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(self);
obj.fmt(f)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/runtime/nsproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::fmt;
use core::hash;

use crate::mutability::Root;
use crate::runtime::{AnyClass, AnyObject, NSObject, NSObjectProtocol, ProtocolObject};
use crate::runtime::{AnyClass, AnyObject, NSObjectProtocol, ProtocolObject};
use crate::ClassType;

crate::__emit_struct! {
Expand Down Expand Up @@ -97,7 +97,7 @@ impl fmt::Debug for NSProxy {
#[doc(alias = "description")]
#[doc(alias = "debugDescription")]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let obj: &ProtocolObject<NSObject> = ProtocolObject::from_ref(self);
let obj: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(self);
obj.fmt(f)
}
}
18 changes: 9 additions & 9 deletions crates/objc2/src/runtime/protocol_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,39 +272,39 @@ mod tests {
#[test]
fn impl_traits() {
assert_impl_all!(NSObject: NSObjectProtocol);
assert_impl_all!(ProtocolObject<NSObject>: NSObjectProtocol);
assert_impl_all!(ProtocolObject<dyn NSObjectProtocol>: NSObjectProtocol);
assert_not_impl_any!(ProtocolObject<dyn Foo>: NSObjectProtocol);
assert_impl_all!(ProtocolObject<dyn Bar>: NSObjectProtocol);
assert_impl_all!(ProtocolObject<dyn FooBar>: NSObjectProtocol);
assert_impl_all!(ProtocolObject<dyn FooFooBar>: NSObjectProtocol);
assert_impl_all!(DummyClass: NSObjectProtocol);

assert_not_impl_any!(NSObject: Foo);
assert_not_impl_any!(ProtocolObject<NSObject>: Foo);
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: Foo);
assert_impl_all!(ProtocolObject<dyn Foo>: Foo);
assert_not_impl_any!(ProtocolObject<dyn Bar>: Foo);
assert_impl_all!(ProtocolObject<dyn FooBar>: Foo);
assert_impl_all!(ProtocolObject<dyn FooFooBar>: Foo);
assert_impl_all!(DummyClass: Foo);

assert_not_impl_any!(NSObject: Bar);
assert_not_impl_any!(ProtocolObject<NSObject>: Bar);
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: Bar);
assert_not_impl_any!(ProtocolObject<dyn Foo>: Bar);
assert_impl_all!(ProtocolObject<dyn Bar>: Bar);
assert_impl_all!(ProtocolObject<dyn FooBar>: Bar);
assert_impl_all!(ProtocolObject<dyn FooFooBar>: Bar);
assert_impl_all!(DummyClass: Bar);

assert_not_impl_any!(NSObject: FooBar);
assert_not_impl_any!(ProtocolObject<NSObject>: FooBar);
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: FooBar);
assert_not_impl_any!(ProtocolObject<dyn Foo>: FooBar);
assert_not_impl_any!(ProtocolObject<dyn Bar>: FooBar);
assert_impl_all!(ProtocolObject<dyn FooBar>: FooBar);
assert_impl_all!(ProtocolObject<dyn FooFooBar>: FooBar);
assert_impl_all!(DummyClass: FooBar);

assert_not_impl_any!(NSObject: FooFooBar);
assert_not_impl_any!(ProtocolObject<NSObject>: FooFooBar);
assert_not_impl_any!(ProtocolObject<dyn NSObjectProtocol>: FooFooBar);
assert_not_impl_any!(ProtocolObject<dyn Foo>: FooFooBar);
assert_not_impl_any!(ProtocolObject<dyn Bar>: FooFooBar);
assert_not_impl_any!(ProtocolObject<dyn FooBar>: FooFooBar);
Expand All @@ -326,10 +326,10 @@ mod tests {
let foo: &ProtocolObject<dyn Foo> = ProtocolObject::from_ref(&*obj);
let _foo: &ProtocolObject<dyn Foo> = ProtocolObject::from_ref(foo);

let _nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(foobar);
let _nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(bar);
let nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(&*obj);
let _nsobject: &ProtocolObject<NSObject> = ProtocolObject::from_ref(nsobject);
let _nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(foobar);
let _nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(bar);
let nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(&*obj);
let _nsobject: &ProtocolObject<dyn NSObjectProtocol> = ProtocolObject::from_ref(nsobject);

let _foobar: &mut ProtocolObject<dyn FooBar> = ProtocolObject::from_mut(&mut *obj);
let _foobar: Id<ProtocolObject<dyn FooBar>> = ProtocolObject::from_id(obj);
Expand Down
4 changes: 2 additions & 2 deletions crates/tests/src/test_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,6 @@ fn test_protocol() {
#[cfg(feature = "Foundation_all")]
assert_eq!(MyTestObject::h().as_i32(), 8);

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

0 comments on commit 864365e

Please sign in to comment.