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>

source

pub fn from_ref<T: ?Sized + Message>(obj: &T) -> &Self
where P: ImplementedBy<T>,

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

source

pub fn from_mut<T: ?Sized + Message>(obj: &mut T) -> &mut Self
where P: ImplementedBy<T>,

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

source

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.

source

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>

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: ?Sized, T> AsRef<ProtocolObject<T>> for 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> CounterpartOrSelf for 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>

The mutable counterpart of the type, or Self if the type has no mutable counterpart. Read more
source§

impl<P: ?Sized + 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 + 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 + NSObjectProtocol,

source§

fn isEqual(&self, other: &AnyObject) -> bool
where Self: Sized + Message,

Check whether the object is equal to an arbitrary other object. Read more
source§

fn hash(&self) -> NSUInteger
where Self: Sized + Message,

An integer that can be used as a table address in a hash table structure. Read more
source§

fn isKindOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

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
where Self: Sized + Message,

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
where Self: Sized + Message,

Check if the object is an instance of a specific class, without checking subclasses. Read more
source§

fn respondsToSelector(&self, aSelector: Sel) -> bool
where Self: Sized + Message,

Check whether the object implements or inherits a method with the given selector. Read more
source§

fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
where Self: Sized + Message,

Check whether the object conforms to a given protocol. Read more
source§

fn description(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object. Read more
source§

fn debugDescription(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object to use when debugging. Read more
source§

fn isProxy(&self) -> bool
where Self: Sized + Message,

Check whether the receiver is a subclass of the NSProxy root class instead of the usual NSObject. Read more
source§

fn retainCount(&self) -> NSUInteger
where Self: Sized + Message,

The reference count of the object. Read more
source§

impl<P: ?Sized + NSObjectProtocol> PartialEq 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> 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 + NSObjectProtocol> Eq for ProtocolObject<P>

source§

impl<P: ?Sized + HasStableHash> HasStableHash for ProtocolObject<P>

source§

impl<P: ?Sized + IsAllocableAnyThread> IsAllocableAnyThread for ProtocolObject<P>

source§

impl<P: ?Sized + IsAllowedMutable> IsAllowedMutable for ProtocolObject<P>

source§

impl<P: ?Sized + IsIdCloneable> IsIdCloneable for ProtocolObject<P>

source§

impl<P: ?Sized + IsMainThreadOnly> IsMainThreadOnly for ProtocolObject<P>

source§

impl<P: ?Sized + IsMutable> IsMutable for ProtocolObject<P>

source§

impl<P: ?Sized + IsRetainable> IsRetainable for ProtocolObject<P>

source§

impl<P: ?Sized> Message for ProtocolObject<P>

source§

impl<P: ?Sized + Send> Send for ProtocolObject<P>

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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 T
where T: ?Sized,