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::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_mut<T: ?Sized + Message>(obj: &mut T) -> &mut Selfwhere
P: ImplementedBy<T>,
pub fn from_mut<T: ?Sized + Message>(obj: &mut T) -> &mut Selfwhere
P: ImplementedBy<T>,
Get a mutable 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,
pub fn from_id<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.
Soft-deprecated alias of ProtocolObject::from_retained
.
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> 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>
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> CounterpartOrSelf for ProtocolObject<P>
impl<P: ?Sized> CounterpartOrSelf for ProtocolObject<P>
§type Immutable = ProtocolObject<P>
type Immutable = ProtocolObject<P>
Self
if the type has no
immutable counterpart. Read more§type Mutable = ProtocolObject<P>
type Mutable = ProtocolObject<P>
Self
if the type has no
mutable counterpart. Read more