Trait DowncastTarget

Source
pub unsafe trait DowncastTarget: ClassType + 'static { }
Expand description

Classes that can be safely downcasted to.

DowncastTarget is an unsafe marker trait that can be implemented on types that also implement ClassType.

Ideally, every type that implements ClassType would also be a valid downcast target, however this would be unsound when used with generics, because we can only trivially decide whether the “base container” is an instance of some class type, but anything related to the generic arguments is unknown.

This trait is implemented automatically by the extern_class! and define_class! macros.

§Safety

The type must not have any generic arguments other than AnyObject.

§Examples

Implementing DowncastTarget for NSString:

// SAFETY: NSString does not have any generic parameters.
unsafe impl DowncastTarget for NSString {}

However, implementing it for NSArray can only be done when the object type is AnyObject.

// SAFETY: NSArray does not have any generic parameters set (the generic
// defaults to `AnyObject`).
unsafe impl DowncastTarget for NSArray {}

// This would not be valid, since downcasting can only trivially determine
// whether the base class (in this case `NSArray`) matches the receiver
// class type.
// unsafe impl<T: Message> DowncastTarget for NSArray<T> {}

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.

Implementors§