diff --git a/crates/objc2/CHANGELOG.md b/crates/objc2/CHANGELOG.md index e604ad0f5..ce13dae31 100644 --- a/crates/objc2/CHANGELOG.md +++ b/crates/objc2/CHANGELOG.md @@ -149,6 +149,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * **BREAKING**: Make `rc::Allocated` allowed to be `NULL` internally, such that uses of `Option>` is now simply `Allocated`. * `AnyObject::class` now returns a `'static` reference to the class. +* Relaxed `ProtocolType` requirement on `ProtocolObject`. ### Deprecated * Soft deprecated using `msg_send!` without a comma between arguments (i.e. diff --git a/crates/objc2/src/macros/extern_protocol.rs b/crates/objc2/src/macros/extern_protocol.rs index 5418fb52a..9771871cb 100644 --- a/crates/objc2/src/macros/extern_protocol.rs +++ b/crates/objc2/src/macros/extern_protocol.rs @@ -184,7 +184,7 @@ macro_rules! __inner_extern_protocol { $(#[$impl_m])* unsafe impl $name for $crate::runtime::ProtocolObject where - T: ?$crate::__macro_helpers::Sized + $crate::ProtocolType + $name + T: ?$crate::__macro_helpers::Sized + $name {} // SAFETY: The specified name is ensured by caller to be a protocol, diff --git a/crates/objc2/src/mutability.rs b/crates/objc2/src/mutability.rs index f2d707ecb..0bc764084 100644 --- a/crates/objc2/src/mutability.rs +++ b/crates/objc2/src/mutability.rs @@ -33,7 +33,7 @@ use core::marker::PhantomData; use crate::runtime::{AnyObject, ProtocolObject}; -use crate::{ClassType, Message, ProtocolType}; +use crate::{ClassType, Message}; mod private_mutability { pub trait Sealed {} @@ -269,7 +269,7 @@ mod private_traits { } impl private_traits::Sealed for T {} -impl private_traits::Sealed for ProtocolObject

{} +impl private_traits::Sealed for ProtocolObject

{} impl private_traits::Sealed for AnyObject {} /// Marker trait for classes where [`Id::clone`] is safe. @@ -303,7 +303,7 @@ impl MutabilityIsIdCloneable for InteriorMutable {} impl MutabilityIsIdCloneable for MainThreadOnly {} unsafe impl IsIdCloneable for T where T::Mutability: MutabilityIsIdCloneable {} -unsafe impl IsIdCloneable for ProtocolObject

{} +unsafe impl IsIdCloneable for ProtocolObject

{} // SAFETY: Same as for root classes. unsafe impl IsIdCloneable for AnyObject {} @@ -336,7 +336,7 @@ impl MutabilityIsRetainable for InteriorMutable {} impl MutabilityIsRetainable for MainThreadOnly {} unsafe impl IsRetainable for T where T::Mutability: MutabilityIsRetainable {} -unsafe impl IsRetainable for ProtocolObject

{} +unsafe impl IsRetainable for ProtocolObject

{} /// Marker trait for classes that can be allocated from any thread. /// @@ -367,10 +367,7 @@ unsafe impl IsAllocableAnyThread for T where T::Mutability: MutabilityIsAllocableAnyThread { } -unsafe impl IsAllocableAnyThread - for ProtocolObject

-{ -} +unsafe impl IsAllocableAnyThread for ProtocolObject

{} /// Marker trait for classes that may feasibly be used behind a mutable /// reference. @@ -401,7 +398,7 @@ unsafe impl IsAllowedMutable for T where T::Mutability: MutabilityIsAllowedMutable { } -unsafe impl IsAllowedMutable for ProtocolObject

{} +unsafe impl IsAllowedMutable for ProtocolObject

{} // SAFETY: Same as for root classes. unsafe impl IsAllowedMutable for AnyObject {} @@ -430,7 +427,7 @@ impl MutabilityIsMutable for Mutable {} impl MutabilityIsMutable for MutableWithImmutableSuperclass {} unsafe impl IsMutable for T where T::Mutability: MutabilityIsMutable {} -unsafe impl IsMutable for ProtocolObject

{} +unsafe impl IsMutable for ProtocolObject

{} /// Marker trait for classes that are only available on the main thread. /// @@ -455,7 +452,7 @@ unsafe impl IsMainThreadOnly for T where T::Mutability: MutabilityIsMainThreadOnly { } -unsafe impl IsMainThreadOnly for ProtocolObject

{} +unsafe impl IsMainThreadOnly for ProtocolObject

{} /// Marker trait for classes whose `hash` and `isEqual:` methods are stable. /// @@ -489,7 +486,7 @@ impl MutabilityHashIsStable for ImmutableWithMutableSubclass {} impl MutabilityHashIsStable for MutableWithImmutableSuperclass {} unsafe impl HasStableHash for T where T::Mutability: MutabilityHashIsStable {} -unsafe impl HasStableHash for ProtocolObject

{} +unsafe impl HasStableHash for ProtocolObject

{} /// Retrieve the immutable/mutable counterpart class, and fall back to `Self` /// if not applicable. @@ -589,7 +586,7 @@ where type Mutable = >::Mutable; } -unsafe impl CounterpartOrSelf for ProtocolObject

{ +unsafe impl CounterpartOrSelf for ProtocolObject

{ // SAFETY: The only place where this would differ from `Self` is for // classes with `MutableWithImmutableSuperclass`. // diff --git a/crates/objc2/src/runtime/protocol_object.rs b/crates/objc2/src/runtime/protocol_object.rs index 46ff46c29..62a9b40d1 100644 --- a/crates/objc2/src/runtime/protocol_object.rs +++ b/crates/objc2/src/runtime/protocol_object.rs @@ -7,7 +7,7 @@ use crate::encode::{Encoding, RefEncode}; use crate::rc::{autoreleasepool_leaking, Id}; use crate::runtime::__nsstring::nsstring_to_str; use crate::runtime::{AnyObject, NSObjectProtocol}; -use crate::{Message, ProtocolType}; +use crate::Message; /// An internal helper trait for [`ProtocolObject`]. /// @@ -57,25 +57,25 @@ pub unsafe trait ImplementedBy { /// ``` #[doc(alias = "id")] #[repr(C)] -pub struct ProtocolObject { +pub struct ProtocolObject { inner: AnyObject, p: PhantomData

, } // SAFETY: The type is `#[repr(C)]` and `AnyObject` internally -unsafe impl RefEncode for ProtocolObject

{ +unsafe impl RefEncode for ProtocolObject

{ const ENCODING_REF: Encoding = Encoding::Object; } // SAFETY: The type is `AnyObject` internally, and is mean to be messaged // as-if it's an object. -unsafe impl Message for ProtocolObject

{} +unsafe impl Message for ProtocolObject

{} -impl ProtocolObject

{ +impl ProtocolObject

{ /// Get an immutable type-erased reference from a type implementing a /// protocol. #[inline] - pub fn from_ref(obj: &T) -> &Self + pub fn from_ref(obj: &T) -> &Self where P: ImplementedBy, { @@ -89,7 +89,7 @@ impl ProtocolObject

{ /// Get a mutable type-erased reference from a type implementing a /// protocol. #[inline] - pub fn from_mut(obj: &mut T) -> &mut Self + pub fn from_mut(obj: &mut T) -> &mut Self where P: ImplementedBy, { @@ -118,7 +118,7 @@ impl ProtocolObject

{ } } -impl PartialEq for ProtocolObject

{ +impl PartialEq for ProtocolObject

{ #[inline] #[doc(alias = "isEqual:")] fn eq(&self, other: &Self) -> bool { @@ -126,16 +126,16 @@ impl PartialEq for ProtocolObject

Eq for ProtocolObject

{} +impl Eq for ProtocolObject

{} -impl hash::Hash for ProtocolObject

{ +impl hash::Hash for ProtocolObject

{ #[inline] fn hash(&self, state: &mut H) { self.__hash().hash(state); } } -impl fmt::Debug for ProtocolObject

{ +impl fmt::Debug for ProtocolObject

{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Attempt to format description string if let Some(description) = self.__description() { @@ -159,10 +159,9 @@ impl fmt::Debug for ProtocolObject< } } -impl AsRef> for ProtocolObject

+impl AsRef> for ProtocolObject

where - P: ?Sized + ProtocolType, - T: ?Sized + ProtocolType + ImplementedBy>, + T: ?Sized + ImplementedBy>, { #[inline] fn as_ref(&self) -> &ProtocolObject { @@ -170,10 +169,9 @@ where } } -impl AsMut> for ProtocolObject

+impl AsMut> for ProtocolObject

where - P: ?Sized + ProtocolType, - T: ?Sized + ProtocolType + ImplementedBy>, + T: ?Sized + ImplementedBy>, { #[inline] fn as_mut(&mut self) -> &mut ProtocolObject { @@ -181,7 +179,7 @@ where } } -// TODO: Maybe iplement Borrow + BorrowMut? +// TODO: Maybe implement Borrow + BorrowMut? #[cfg(test)] #[allow(clippy::missing_safety_doc)] @@ -194,7 +192,9 @@ mod tests { use super::*; use crate::mutability::Mutable; use crate::runtime::{NSObject, NSObjectProtocol}; - use crate::{declare_class, extern_methods, extern_protocol, ClassType, DeclaredClass}; + use crate::{ + declare_class, extern_methods, extern_protocol, ClassType, DeclaredClass, ProtocolType, + }; extern_protocol!( unsafe trait Foo {