pub unsafe trait ClassType: Message {
type Super: Message;
type ThreadKind: ?Sized + ThreadKind;
const NAME: &'static str;
// Required methods
fn class() -> &'static AnyClass;
fn as_super(&self) -> &Self::Super;
}
Expand description
Marks types that represent specific classes.
Sometimes it is enough to generically know that a type is messageable,
e.g. Retained
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, as well as
a few properties of the class to the rest of the type-system.
This is implemented for your type by the
define_class!
and
extern_class!
macros.
§Safety
This is meant to be a sealed trait, and should not be implemented outside of the aforementioned macros. See those for safety preconditions.
§Examples
Use the trait to access the AnyClass
of an object.
use objc2::{ClassType, msg_send};
use objc2::rc::Retained;
// Get the class of the object.
let cls = <MyObject as ClassType>::class();
// Or, since the trait is in scope.
let cls = MyObject::class();
// We can now access properties of the class.
assert_eq!(cls.name().to_str().unwrap(), MyObject::NAME);
// And we can send messages to the class.
//
// SAFETY:
// - The class is `MyObject`, which can safely be initialized with `new`.
// - The return type is correctly specified.
let obj: Retained<MyObject> = unsafe { msg_send![cls, new] };
Use the trait to allocate a new instance of an object.
use objc2::{msg_send, AnyThread};
use objc2::rc::Retained;
let obj = MyObject::alloc();
// Now we can call initializers on this newly allocated object.
//
// SAFETY: `MyObject` can safely be initialized with `init`.
let obj: Retained<MyObject> = unsafe { msg_send![obj, init] };
Use the extern_class!
macro to implement this
trait for a type.
use objc2::runtime::NSObject;
use objc2::{extern_class, ClassType, AnyThread};
extern_class!(
// SAFETY: The superclass is correctly specified, and the class can be
// safely used from any thread.
#[unsafe(super(NSObject))]
struct MyClass;
);
let cls = MyClass::class();
let obj = MyClass::alloc();
Required Associated Constants§
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 AnyObject
if the class is a root class.
Sourcetype ThreadKind: ?Sized + ThreadKind
type ThreadKind: ?Sized + ThreadKind
Whether the type can be used from any thread, or from only the main thread.
One of dyn AnyThread
or dyn MainThreadOnly
.
Setting this makes ClassType
provide an implementation of either
AnyThread
or MainThreadOnly
.
Required Methods§
Sourcefn class() -> &'static AnyClass
fn class() -> &'static AnyClass
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 creating the class, e.g. if the program is not properly linked to the framework that defines the class.
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.