MCPcopy Index your code
hub / github.com/SSheldon/rust-objc

github.com/SSheldon/rust-objc @0.2.7

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.2.7 ↗ · + Follow
155 symbols 306 edges 21 files 56 documented · 36%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Objective-C Runtime bindings and wrapper for Rust.

  • Documentation: http://ssheldon.github.io/rust-objc/objc/
  • Crate: https://crates.io/crates/objc

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

The utilities of the rc module provide ARC-like semantics for working with Objective-C's reference counted objects in Rust. A StrongPtr retains an object and releases the object when dropped. A WeakPtr will not retain the object, but can be upgraded to a StrongPtr and safely fails if the object has been deallocated.

// StrongPtr will release the object when dropped
let obj = unsafe {
    StrongPtr::new(msg_send![class!(NSObject), new])
};

// Cloning retains the object an additional time
let cloned = obj.clone();
autoreleasepool(|| {
    // Autorelease consumes the StrongPtr, but won't
    // actually release until the end of an autoreleasepool
    cloned.autorelease();
});

// Weak references won't retain the object
let weak = obj.weak();
drop(obj);
assert!(weak.load().is_null());

Declaring classes

Classes can be declared using the ClassDecl struct. Instance variables and methods can then be added before the class is ultimately registered.

The following example demonstrates declaring a class named MyNumber that has one ivar, a u32 named _number and a number method that returns it:

let superclass = class!(NSObject);
let mut decl = ClassDecl::new("MyNumber", superclass).unwrap();

// Add an instance variable
decl.add_ivar::<u32>("_number");

// Add an ObjC method for getting the number
extern fn my_number_get(this: &Object, _cmd: Sel) -> u32 {
    unsafe { *this.get_ivar("_number") }
}
unsafe {
    decl.add_method(sel!(number),
        my_number_get as extern fn(&Object, Sel) -> u32);
}

decl.register();

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.

Extension points exported contracts — how you extend this code

Encode (Interface)
Types that have an Objective-C type encoding. Unsafe because Objective-C will make assumptions about the type (like its [3 …
src/encode.rs
Message (Interface)
Types that may be sent Objective-C messages. For example: objects, classes, and blocks. [2 implementers]
src/message/mod.rs
MethodImplementation (Interface)
Types that can be used as the implementation of an Objective-C method.
src/declare.rs
EncodeArguments (Interface)
Types that represent a group of arguments, where each has an Objective-C type encoding.
src/encode.rs
MessageArguments (Interface)
Types that may be used as the arguments of an Objective-C message.
src/message/mod.rs

Core symbols most depended-on inside this repo

as_ptr
called by 11
src/runtime.rs
as_str
called by 9
src/encode.rs
from_str
called by 6
src/encode.rs
get
called by 5
src/runtime.rs
add_method
called by 5
src/declare.rs
register
called by 4
src/runtime.rs
offset
called by 4
src/runtime.rs
class
called by 4
src/runtime.rs

Shape

Method 71
Function 62
Class 16
Interface 5
Enum 1

Languages

Rust100%

Modules by API surface

src/runtime.rs45 symbols
src/test_utils.rs21 symbols
src/declare.rs20 symbols
src/encode.rs16 symbols
src/message/mod.rs15 symbols
src/rc/strong.rs9 symbols
src/rc/weak.rs5 symbols
src/rc/mod.rs5 symbols
src/rc/autorelease.rs4 symbols
src/message/gnustep.rs2 symbols
src/message/apple/x86_64.rs2 symbols
src/message/apple/x86.rs2 symbols

For agents

$ claude mcp add rust-objc \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact