#[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>

source

pub fn from_ref<T: Message>(obj: &T) -> &Selfwhere P: ImplementedBy<T>,

Get an immutable type-erased reference from a type implementing a protocol.

source

pub fn from_mut<T: Message>(obj: &mut T) -> &mut Selfwhere P: ImplementedBy<T>,

Get a mutable type-erased reference from a type implementing a protocol.

source

pub fn from_id<T>(obj: Id<T>) -> Id<Self> where P: ImplementedBy<T> + 'static, T: 'static + Message,

Get a type-erased object 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>>,

source§

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>>,

source§

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>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<P: ?Sized + ProtocolType + NSObjectProtocol> Hash for ProtocolObject<P>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T> NSObjectProtocol for ProtocolObject<T>where T: ?Sized + ProtocolType + NSObjectProtocol,

source§

fn is_kind_of<T: ClassType>(&self) -> boolwhere Self: Sized + Message,

Check if the object is an instance of the class, or one of it’s subclasses. Read more
source§

impl<P: ?Sized + ProtocolType + NSObjectProtocol> PartialEq<ProtocolObject<P>> for ProtocolObject<P>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<P: ?Sized + ProtocolType> RefEncode for ProtocolObject<P>

source§

const ENCODING_REF: Encoding = Encoding::Object

The Objective-C type-encoding for a reference of this type. Read more
source§

impl<P: ?Sized + ProtocolType + NSObjectProtocol> Eq for ProtocolObject<P>

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> AutoreleaseSafe for Twhere T: ?Sized,