pub unsafe trait ClassType: Message {
type Super: Message;
const NAME: &'static str;
// Required methods
fn class() -> &'static Class;
fn as_super(&self) -> &Self::Super;
fn as_super_mut(&mut self) -> &mut Self::Super;
}
Expand description
Marks types that represent specific classes.
Usually it is enough to generically know that a type is messageable, e.g.
rc::Id
works with any type that implements the
Message
trait. But often, you have an object that you know represents
a specific Objective-C class - this trait allows you to communicate that
to the rest of the type-system.
This is implemented automatically by the
declare_class!
and
extern_class!
macros.
Safety
The class returned by Self::class
must be a subclass of the class that
Self::Super
represents, and as_super
/as_super_mut
must be
implemented correctly. Finally Self::NAME
must be correct.
In pseudocode:
Self::class().superclass() == <Self::Super as ClassType>::class()
Self::class().name() == Self::NAME
Examples
Use the trait to access the Class
of different objects.
use objc2::ClassType;
use objc2::foundation::NSObject;
// Get a class object representing `NSObject`
let cls = <NSObject as ClassType>::class(); // Or just `NSObject::class()`
Use the extern_class!
macro to implement this
trait for a type.
use objc2::{extern_class, ClassType};
extern_class!(
struct MyClass;
unsafe impl ClassType for MyClass {
type Super = NSObject;
}
);
let cls = MyClass::class();
Required Associated Types§
sourcetype Super: Message
type Super: Message
The superclass of this class.
If you have implemented Deref
for your type, it is highly
recommended that this is equal to Deref::Target
.
This may be runtime::Object
if the class is a root class.
Required Associated Constants§
Required Methods§
sourcefn class() -> &'static Class
fn class() -> &'static Class
Get a reference to the Objective-C class that this type represents.
May register the class with the runtime if it wasn’t already.
Panics
This may panic if something went wrong with getting or declaring the class, e.g. if the program is not properly linked to the framework that defines the class.
sourcefn as_super_mut(&mut self) -> &mut Self::Super
fn as_super_mut(&mut self) -> &mut Self::Super
Get a mutable reference to the superclass.