From 864365ec2562029b59e7e50be4c519186d53a257 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 11 Sep 2023 01:11:09 +0200 Subject: [PATCH] Remove ProtocolType implementation for NSObject It is more like an odd edge-case that `NSObject` implements `ProtocolType` and can be used as `ProtocolObject`, users should instead use `NSObjectProtocol` which works like any other protocol. --- crates/objc2/CHANGELOG.md | 4 ++++ crates/objc2/src/protocol_type.rs | 8 +++---- crates/objc2/src/runtime/nsobject.rs | 24 ++------------------- crates/objc2/src/runtime/nsproxy.rs | 4 ++-- crates/objc2/src/runtime/protocol_object.rs | 18 ++++++++-------- crates/tests/src/test_object.rs | 4 ++-- 6 files changed, 23 insertions(+), 39 deletions(-) diff --git a/crates/objc2/CHANGELOG.md b/crates/objc2/CHANGELOG.md index bf31a9715..2ed8ef40e 100644 --- a/crates/objc2/CHANGELOG.md +++ b/crates/objc2/CHANGELOG.md @@ -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 diff --git a/crates/objc2/src/protocol_type.rs b/crates/objc2/src/protocol_type.rs index 11c8c8b42..8b5580e0b 100644 --- a/crates/objc2/src/protocol_type.rs +++ b/crates/objc2/src/protocol_type.rs @@ -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 = ::protocol().expect("NSObject to have a protocol"); +/// assert_eq!(::NAME, protocol.name()); /// ``` /// /// Use the [`extern_protocol!`] macro to implement this trait for a type. diff --git a/crates/objc2/src/runtime/nsobject.rs b/crates/objc2/src/runtime/nsobject.rs index 73b9c97e9..f188fb09d 100644 --- a/crates/objc2/src/runtime/nsobject.rs +++ b/crates/objc2/src/runtime/nsobject.rs @@ -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}; @@ -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(::NAME) - .expect("could not find NSObject protocol"), - ) - } - - const __INNER: () = (); -} - -unsafe impl ImplementedBy for NSObject -where - T: ?Sized + Message + NSObjectProtocol, -{ - const __INNER: () = (); -} - extern_methods!( unsafe impl NSObject { /// Create a new empty `NSObject`. @@ -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 = ProtocolObject::from_ref(self); + let obj: &ProtocolObject = ProtocolObject::from_ref(self); obj.fmt(f) } } diff --git a/crates/objc2/src/runtime/nsproxy.rs b/crates/objc2/src/runtime/nsproxy.rs index e4ff409c4..cfd87851d 100644 --- a/crates/objc2/src/runtime/nsproxy.rs +++ b/crates/objc2/src/runtime/nsproxy.rs @@ -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! { @@ -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 = ProtocolObject::from_ref(self); + let obj: &ProtocolObject = ProtocolObject::from_ref(self); obj.fmt(f) } } diff --git a/crates/objc2/src/runtime/protocol_object.rs b/crates/objc2/src/runtime/protocol_object.rs index 15e891a57..ae8c8d085 100644 --- a/crates/objc2/src/runtime/protocol_object.rs +++ b/crates/objc2/src/runtime/protocol_object.rs @@ -272,7 +272,7 @@ mod tests { #[test] fn impl_traits() { assert_impl_all!(NSObject: NSObjectProtocol); - assert_impl_all!(ProtocolObject: NSObjectProtocol); + assert_impl_all!(ProtocolObject: NSObjectProtocol); assert_not_impl_any!(ProtocolObject: NSObjectProtocol); assert_impl_all!(ProtocolObject: NSObjectProtocol); assert_impl_all!(ProtocolObject: NSObjectProtocol); @@ -280,7 +280,7 @@ mod tests { assert_impl_all!(DummyClass: NSObjectProtocol); assert_not_impl_any!(NSObject: Foo); - assert_not_impl_any!(ProtocolObject: Foo); + assert_not_impl_any!(ProtocolObject: Foo); assert_impl_all!(ProtocolObject: Foo); assert_not_impl_any!(ProtocolObject: Foo); assert_impl_all!(ProtocolObject: Foo); @@ -288,7 +288,7 @@ mod tests { assert_impl_all!(DummyClass: Foo); assert_not_impl_any!(NSObject: Bar); - assert_not_impl_any!(ProtocolObject: Bar); + assert_not_impl_any!(ProtocolObject: Bar); assert_not_impl_any!(ProtocolObject: Bar); assert_impl_all!(ProtocolObject: Bar); assert_impl_all!(ProtocolObject: Bar); @@ -296,7 +296,7 @@ mod tests { assert_impl_all!(DummyClass: Bar); assert_not_impl_any!(NSObject: FooBar); - assert_not_impl_any!(ProtocolObject: FooBar); + assert_not_impl_any!(ProtocolObject: FooBar); assert_not_impl_any!(ProtocolObject: FooBar); assert_not_impl_any!(ProtocolObject: FooBar); assert_impl_all!(ProtocolObject: FooBar); @@ -304,7 +304,7 @@ mod tests { assert_impl_all!(DummyClass: FooBar); assert_not_impl_any!(NSObject: FooFooBar); - assert_not_impl_any!(ProtocolObject: FooFooBar); + assert_not_impl_any!(ProtocolObject: FooFooBar); assert_not_impl_any!(ProtocolObject: FooFooBar); assert_not_impl_any!(ProtocolObject: FooFooBar); assert_not_impl_any!(ProtocolObject: FooFooBar); @@ -326,10 +326,10 @@ mod tests { let foo: &ProtocolObject = ProtocolObject::from_ref(&*obj); let _foo: &ProtocolObject = ProtocolObject::from_ref(foo); - let _nsobject: &ProtocolObject = ProtocolObject::from_ref(foobar); - let _nsobject: &ProtocolObject = ProtocolObject::from_ref(bar); - let nsobject: &ProtocolObject = ProtocolObject::from_ref(&*obj); - let _nsobject: &ProtocolObject = ProtocolObject::from_ref(nsobject); + let _nsobject: &ProtocolObject = ProtocolObject::from_ref(foobar); + let _nsobject: &ProtocolObject = ProtocolObject::from_ref(bar); + let nsobject: &ProtocolObject = ProtocolObject::from_ref(&*obj); + let _nsobject: &ProtocolObject = ProtocolObject::from_ref(nsobject); let _foobar: &mut ProtocolObject = ProtocolObject::from_mut(&mut *obj); let _foobar: Id> = ProtocolObject::from_id(obj); diff --git a/crates/tests/src/test_object.rs b/crates/tests/src/test_object.rs index 9466ff4a8..abb5fde52 100644 --- a/crates/tests/src/test_object.rs +++ b/crates/tests/src/test_object.rs @@ -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 = ProtocolObject::from_ref(&*proto); + // Check that transforming to `NSObjectProtocol` works + let _obj: &ProtocolObject = ProtocolObject::from_ref(&*proto); }