Struct objc2::runtime::ProtocolObject
source · #[repr(C)]pub struct ProtocolObject<P: ?Sized> { /* private fields */ }Expand description
An object representing any object that implements a specified protocol.
Objective-C has a feature where you can write
id<MyProtocol>, and then work with the protocol as-if it was an object;
this is very similar to dyn traits in Rust!
If we could customize how dyn Trait works, then this struct would not
have been necessary; however, dyn Trait is a wide pointer with overhead,
which this struct helps avoid.
If the trait T inherits NSObjectProtocol, this will implement common
traits like Debug, PartialEq, Eq and Hash.
§Example
Convert an object MyObject that implements the a protocol MyProtocol
into a ProtocolObject for working with the protocol in a type-erased
way.
use objc2::runtime::ProtocolObject;
use objc2::rc::Id;
let obj: Id<MyObject> = MyObject::new();
let proto: &ProtocolObject<dyn MyProtocol> = ProtocolObject::from_ref(&*obj);
let proto: Id<ProtocolObject<dyn MyProtocol>> = ProtocolObject::from_id(obj);Implementations§
source§impl<P: ?Sized> ProtocolObject<P>
impl<P: ?Sized> ProtocolObject<P>
sourcepub fn from_ref<T: ?Sized + Message>(obj: &T) -> &Selfwhere
P: ImplementedBy<T>,
pub fn from_ref<T: ?Sized + Message>(obj: &T) -> &Selfwhere
P: ImplementedBy<T>,
Get an immutable type-erased reference from a type implementing a protocol.
Trait Implementations§
source§impl<P: ?Sized, T> AsMut<ProtocolObject<T>> for ProtocolObject<P>
impl<P: ?Sized, T> AsMut<ProtocolObject<T>> for ProtocolObject<P>
source§fn as_mut(&mut self) -> &mut ProtocolObject<T>
fn as_mut(&mut self) -> &mut ProtocolObject<T>
Converts this type into a mutable reference of the (usually inferred) input type.
source§impl<P: ?Sized, T> AsRef<ProtocolObject<T>> for ProtocolObject<P>
impl<P: ?Sized, T> AsRef<ProtocolObject<T>> for ProtocolObject<P>
source§fn as_ref(&self) -> &ProtocolObject<T>
fn as_ref(&self) -> &ProtocolObject<T>
Converts this type into a shared reference of the (usually inferred) input type.
source§impl<P: ?Sized> CounterpartOrSelf for ProtocolObject<P>
impl<P: ?Sized> CounterpartOrSelf for ProtocolObject<P>
§type Immutable = ProtocolObject<P>
type Immutable = ProtocolObject<P>
The immutable counterpart of the type, or
Self if the type has no
immutable counterpart. Read more§type Mutable = ProtocolObject<P>
type Mutable = ProtocolObject<P>
The mutable counterpart of the type, or
Self if the type has no
mutable counterpart. Read moresource§impl<P: ?Sized + NSObjectProtocol> Debug for ProtocolObject<P>
impl<P: ?Sized + NSObjectProtocol> Debug for ProtocolObject<P>
source§impl<P: ?Sized + NSObjectProtocol> Hash for ProtocolObject<P>
impl<P: ?Sized + NSObjectProtocol> Hash for ProtocolObject<P>
source§impl<T> NSObjectProtocol for ProtocolObject<T>where
T: ?Sized + NSObjectProtocol,
impl<T> NSObjectProtocol for ProtocolObject<T>where
T: ?Sized + NSObjectProtocol,
source§fn isEqual(&self, other: &AnyObject) -> bool
fn isEqual(&self, other: &AnyObject) -> bool
Check whether the object is equal to an arbitrary other object. Read more
source§fn hash(&self) -> NSUInteger
fn hash(&self) -> NSUInteger
An integer that can be used as a table address in a hash table
structure. Read more
source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Check if the object is an instance of the class, or one of its
subclasses. Read more
source§fn is_kind_of<T: ClassType>(&self) -> bool
fn is_kind_of<T: ClassType>(&self) -> bool
Check if the object is an instance of the class type, or one of its
subclasses. Read more
source§fn isMemberOfClass(&self, cls: &AnyClass) -> bool
fn isMemberOfClass(&self, cls: &AnyClass) -> bool
Check if the object is an instance of a specific class, without
checking subclasses. Read more
source§fn respondsToSelector(&self, aSelector: Sel) -> bool
fn respondsToSelector(&self, aSelector: Sel) -> bool
Check whether the object implements or inherits a method with the
given selector. Read more
source§fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
Check whether the object conforms to a given protocol. Read more
source§fn debugDescription(&self) -> Id<NSObject> ⓘ
fn debugDescription(&self) -> Id<NSObject> ⓘ
A textual representation of the object to use when debugging. Read more
source§fn retainCount(&self) -> NSUInteger
fn retainCount(&self) -> NSUInteger
The reference count of the object. Read more
source§impl<P: ?Sized + NSObjectProtocol> PartialEq for ProtocolObject<P>
impl<P: ?Sized + NSObjectProtocol> PartialEq for ProtocolObject<P>
source§impl<P: ?Sized> RefEncode for ProtocolObject<P>
impl<P: ?Sized> RefEncode for ProtocolObject<P>
source§const ENCODING_REF: Encoding = Encoding::Object
const ENCODING_REF: Encoding = Encoding::Object
The Objective-C type-encoding for a reference of this type. Read more
impl<P: ?Sized + NSObjectProtocol> Eq for ProtocolObject<P>
impl<P: ?Sized + HasStableHash> HasStableHash for ProtocolObject<P>
impl<P: ?Sized + IsAllocableAnyThread> IsAllocableAnyThread for ProtocolObject<P>
impl<P: ?Sized + IsAllowedMutable> IsAllowedMutable for ProtocolObject<P>
impl<P: ?Sized + IsIdCloneable> IsIdCloneable for ProtocolObject<P>
impl<P: ?Sized + IsMainThreadOnly> IsMainThreadOnly for ProtocolObject<P>
impl<P: ?Sized + IsMutable> IsMutable for ProtocolObject<P>
impl<P: ?Sized + IsRetainable> IsRetainable for ProtocolObject<P>
impl<P: ?Sized> Message for ProtocolObject<P>
impl<P: ?Sized + Send> Send for ProtocolObject<P>
impl<P: ?Sized + Sync> Sync for ProtocolObject<P>
Auto Trait Implementations§
impl<P> !Freeze for ProtocolObject<P>
impl<P> !RefUnwindSafe for ProtocolObject<P>
impl<P> !Unpin for ProtocolObject<P>
impl<P> !UnwindSafe for ProtocolObject<P>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more