Trait objc2::ProtocolType

source ·
pub unsafe trait ProtocolType {
    const NAME: &'static str;

    // Provided method
    fn protocol() -> Option<&'static AnyProtocol> { ... }
}
Expand description

Marks types that represent specific protocols.

This is the protocol equivalent of ClassType.

This is implemented automatically by the extern_protocol! macro for dyn T, where T is the protocol.

§Safety

This is meant to be a sealed trait, and should not be implemented outside of the extern_protocol! macro.

§Examples

Use the trait to access the AnyProtocol of different objects.

use objc2::ProtocolType;
use objc2::runtime::NSObjectProtocol;
// Get a protocol object representing the `NSObject` protocol
let protocol = <dyn NSObjectProtocol>::protocol().expect("NSObject to have a protocol");
assert_eq!(<dyn NSObjectProtocol>::NAME, protocol.name());

Use the extern_protocol! macro to implement this trait for a type.

use objc2::{extern_protocol, ProtocolType};

extern_protocol!(
    unsafe trait MyProtocol {}
    unsafe impl ProtocolType for dyn MyProtocol {}
);

let protocol = <dyn MyProtocol>::protocol();

Required Associated Constants§

source

const NAME: &'static str

The name of the Objective-C protocol that this type represents.

Provided Methods§

source

fn protocol() -> Option<&'static AnyProtocol>

Get a reference to the Objective-C protocol object that this type represents.

May register the protocol with the runtime if it wasn’t already.

Note that some protocols are not registered with the runtime, depending on various factors. In those cases, this function may return None.

§Panics

This may panic if something went wrong with getting or declaring the protocol, e.g. if the program is not properly linked to the framework that defines the protocol.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl ProtocolType for dyn NSObjectProtocol

source§

const NAME: &'static str = "NSObject"