#[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::Retained;
let obj: Retained<MyObject> = MyObject::new();
let proto: &ProtocolObject<dyn MyProtocol> = ProtocolObject::from_ref(&*obj);
let proto: Retained<ProtocolObject<dyn MyProtocol>> = ProtocolObject::from_retained(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.
Sourcepub fn from_id<T>(obj: Retained<T>) -> Retained<Self> ⓘwhere
P: ImplementedBy<T> + 'static,
T: Message + 'static,
👎Deprecated: use ProtocolObject::from_retained
instead
pub fn from_id<T>(obj: Retained<T>) -> Retained<Self> ⓘwhere
P: ImplementedBy<T> + 'static,
T: Message + 'static,
ProtocolObject::from_retained
insteadGet a type-erased object from a type implementing a protocol.
Sourcepub fn from_retained<T>(obj: Retained<T>) -> Retained<Self> ⓘwhere
P: ImplementedBy<T> + 'static,
T: Message + 'static,
pub fn from_retained<T>(obj: Retained<T>) -> Retained<Self> ⓘwhere
P: ImplementedBy<T> + 'static,
T: Message + 'static,
Get a type-erased object from a type implementing a protocol.
Trait Implementations§
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>
Source§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<P: ?Sized + MainThreadOnly> MainThreadOnly for ProtocolObject<P>
impl<P: ?Sized + MainThreadOnly> MainThreadOnly for ProtocolObject<P>
Source§fn mtm(&self) -> MainThreadMarker
fn mtm(&self) -> MainThreadMarker
MainThreadMarker
from the main-thread-only object. Read moreSource§impl<P: ?Sized> Message for ProtocolObject<P>
impl<P: ?Sized> Message 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: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&AnyObject>) -> bool
Source§fn hash(&self) -> NSUInteger
fn hash(&self) -> NSUInteger
Source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Source§fn is_kind_of<T: ClassType>(&self) -> bool
fn is_kind_of<T: ClassType>(&self) -> bool
isKindOfClass
directly, or cast your objects with AnyObject::downcast_ref