Crate objc

source ·
Expand description

Objective-C Runtime bindings and wrapper for Rust.

Messaging objects

Objective-C objects can be messaged using the msg_send! macro:

let cls = class!(NSObject);
let obj: *mut Object = msg_send![cls, new];
let hash: usize = msg_send![obj, hash];
let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
// Even void methods must have their return type annotated
let _: () = msg_send![obj, release];

Reference counting

Utilities for reference counting Objective-C objects are provided in the rc module.

Declaring classes

Objective-C classes can even be declared from Rust using the functionality of the declare module.

Exceptions

By default, if the msg_send! macro causes an exception to be thrown, this will unwind into Rust resulting in unsafe, undefined behavior. However, this crate has an "exception" feature which, when enabled, wraps each msg_send! in a @try/@catch and panics if an exception is caught, preventing Objective-C from unwinding into Rust.

Message type verification

The Objective-C runtime includes encodings for each method that describe the argument and return types. This crate can take advantage of these encodings to verify that the types used in Rust match the types encoded for the method.

To use this functionality, enable the "verify_message" feature. With this feature enabled, type checking is performed for every message send, which also requires that all arguments and return values for all messages implement Encode.

If this requirement is burdensome or you’d rather just verify specific messages, you can call the Message::verify_message method for specific selectors.

Support for other Operating Systems

The bindings can be used on Linux or *BSD utilizing the GNUstep Objective-C runtime.

Re-exports

  • pub use message::send_message as __send_message;
  • pub use message::send_super_message as __send_super_message;

Modules

  • Functionality for declaring Objective-C classes.
  • Utilities for reference counting Objective-C objects.
  • A Rust interface for the functionality of the Objective-C runtime.

Macros

  • Gets a reference to a Class.
  • Sends a message to an object.
  • Registers a selector, returning a Sel.

Structs

  • An Objective-C type encoding.
  • An error encountered while attempting to send a message.

Traits

  • Types that have an Objective-C type encoding.
  • Types that represent a group of arguments, where each has an Objective-C type encoding.
  • Types that may be sent Objective-C messages. For example: objects, classes, and blocks.
  • Types that may be used as the arguments of an Objective-C message.