Trait objc2::Message

source ·
pub unsafe trait Message: RefEncode { }
Expand description

Types that can be sent Objective-C messages.

Implementing this provides MessageReceiver implementations for common pointer types and references to the type, which allows using them as the receiver (first argument) in the msg_send! macro.

This trait also allows the object to be used in rc::Id.

This is a subtrait of RefEncode, meaning the type must also implement that, almost always with RefEncode::ENCODING_REF being Encoding::Object.

This can be implemented for unsized (!Sized) types, but the intention is not to support dynamically sized types like slices, only extern types (which is currently unstable).

Drop interaction

If the inner type implements Drop, that implementation will very likely not be called, since there is no way to ensure that the Objective-C runtime will do so. If you need to run some code when the object is destroyed, implement the dealloc method instead.

The declare_class! macro does this for you, but the extern_class! macro fundamentally cannot.

Safety

The type must represent an Objective-C object, meaning it:

  • Must be valid to reinterpret as AnyObject.
  • Must be able to be the receiver of an Objective-C message sent with objc_msgSend or similar.
  • Must respond to the standard memory management retain, release and autorelease messages.
  • Must support weak references. (In the future we should probably make a new trait for this, for example NSTextView only supports weak references on macOS 10.12 or above).

Example

use objc2::runtime::NSObject;
use objc2::{Encoding, Message, RefEncode};

#[repr(C)]
struct MyObject {
    // This has the exact same layout as `NSObject`
    inner: NSObject
}

unsafe impl RefEncode for MyObject {
    const ENCODING_REF: Encoding = Encoding::Object;
}

unsafe impl Message for MyObject {}

// `*mut MyObject` and other pointer/reference types to the object can
// now be used in `msg_send!`
//
// And `Id<MyObject>` can now be constructed.

Object Safety§

This trait is not object safe.

Implementors§