Struct objc2::runtime::ProtocolObject
source · #[repr(C)]pub struct ProtocolObject<P: ?Sized + ProtocolType> { /* 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 + ProtocolType> ProtocolObject<P>
impl<P: ?Sized + ProtocolType> ProtocolObject<P>
sourcepub fn from_ref<T: Message>(obj: &T) -> &Selfwhere
P: ImplementedBy<T>,
pub fn from_ref<T: Message>(obj: &T) -> &Selfwhere P: ImplementedBy<T>,
Get an immutable type-erased reference from a type implementing a protocol.
Trait Implementations§
source§impl<P, T> AsMut<ProtocolObject<T>> for ProtocolObject<P>where
P: ?Sized + ProtocolType,
T: ?Sized + ProtocolType + ImplementedBy<ProtocolObject<P>>,
impl<P, T> AsMut<ProtocolObject<T>> for ProtocolObject<P>where P: ?Sized + ProtocolType, T: ?Sized + ProtocolType + ImplementedBy<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, T> AsRef<ProtocolObject<T>> for ProtocolObject<P>where
P: ?Sized + ProtocolType,
T: ?Sized + ProtocolType + ImplementedBy<ProtocolObject<P>>,
impl<P, T> AsRef<ProtocolObject<T>> for ProtocolObject<P>where P: ?Sized + ProtocolType, T: ?Sized + ProtocolType + ImplementedBy<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 + ProtocolType + NSObjectProtocol> Debug for ProtocolObject<P>
impl<P: ?Sized + ProtocolType + NSObjectProtocol> Debug for ProtocolObject<P>
source§impl<P: ?Sized + ProtocolType + NSObjectProtocol> Hash for ProtocolObject<P>
impl<P: ?Sized + ProtocolType + NSObjectProtocol> Hash for ProtocolObject<P>
source§impl<T> NSObjectProtocol for ProtocolObject<T>where
T: ?Sized + ProtocolType + NSObjectProtocol,
impl<T> NSObjectProtocol for ProtocolObject<T>where T: ?Sized + ProtocolType + NSObjectProtocol,
source§impl<P: ?Sized + ProtocolType + NSObjectProtocol> PartialEq<ProtocolObject<P>> for ProtocolObject<P>
impl<P: ?Sized + ProtocolType + NSObjectProtocol> PartialEq<ProtocolObject<P>> for ProtocolObject<P>
source§impl<P: ?Sized + ProtocolType> RefEncode for ProtocolObject<P>
impl<P: ?Sized + ProtocolType> 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 + ProtocolType + NSObjectProtocol> Eq for ProtocolObject<P>
impl<P: ?Sized + ProtocolType> Message for ProtocolObject<P>
Auto Trait Implementations§
impl<P> !RefUnwindSafe for ProtocolObject<P>
impl<P> !Send for ProtocolObject<P>
impl<P> !Sync 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