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().to_str().unwrap());
Use the extern_protocol!
macro to implement and use this trait.
use objc2::{extern_protocol, ProtocolType};
extern_protocol!(
unsafe trait MyProtocol {}
);
let protocol = <dyn MyProtocol>::protocol();
Required Associated Constants§
Provided Methods§
Sourcefn protocol() -> Option<&'static AnyProtocol>
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 creating the protocol, e.g. if the program is not properly linked to the framework that defines the protocol.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.