Trait objc2::ClassType

source ·
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§

source

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§

source

const NAME: &'static str

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

Required Methods§

source

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.

source

fn as_super(&self) -> &Self::Super

Get an immutable reference to the superclass.

source

fn as_super_mut(&mut self) -> &mut Self::Super

Get a mutable reference to the superclass.

Implementors§

source§

impl ClassType for NSAttributedString

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSBundle

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSData

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSError

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSException

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSMutableAttributedString

§

type Super = NSAttributedString

source§

const NAME: &'static str = _

source§

impl ClassType for NSMutableData

§

type Super = NSData

source§

const NAME: &'static str = _

source§

impl ClassType for NSMutableString

§

type Super = NSString

source§

const NAME: &'static str = _

source§

impl ClassType for NSNumber

§

type Super = NSValue

source§

const NAME: &'static str = _

source§

impl ClassType for NSObject

§

type Super = Object

source§

const NAME: &'static str = "NSObject"

source§

impl ClassType for NSProcessInfo

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSString

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSThread

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSUUID

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl ClassType for NSValue

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl<K: Message, V: Message> ClassType for NSDictionary<K, V>

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl<K: Message, V: Message> ClassType for NSMutableDictionary<K, V>

§

type Super = NSDictionary<K, V>

source§

const NAME: &'static str = _

source§

impl<T: Message, O: Ownership> ClassType for NSArray<T, O>

§

type Super = NSObject

source§

const NAME: &'static str = _

source§

impl<T: Message, O: Ownership> ClassType for NSMutableArray<T, O>

§

type Super = NSArray<T, O>

source§

const NAME: &'static str = _

source§

impl<T: Message, O: Ownership> ClassType for NSMutableSet<T, O>

§

type Super = NSSet<T, O>

source§

const NAME: &'static str = _

source§

impl<T: Message, O: Ownership> ClassType for NSSet<T, O>

§

type Super = NSObject

source§

const NAME: &'static str = _