• Stars
    star
    383
  • Rank 111,995 (Top 3 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 10 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Objective-C Runtime bindings and wrapper for Rust.

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

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.

More Repositories

1

rust-dispatch

Rust wrapper for Apple's Grand Central Dispatch.
Rust
83
star
2

rust-block

Rust interface for Apple's C language extension of blocks.
Rust
48
star
3

Paintbrush

Paintbrush is a Cocoa-based paint program for Mac OS X, similar to Microsoft Paint and MacPaint.
Objective-C
33
star
4

rust-objc-foundation

Rust wrapper for Objective-C's Foundation framework.
Rust
23
star
5

rust-uikit

Rust wrapper for iOS's UIKit framework.
Rust
21
star
6

rust-objc-id

Rust smart pointers for Objective-C reference counting.
Rust
16
star
7

terminated

Types for representing NUL-terminated UTF8 strings in Rust
Rust
10
star
8

rust-objc-encode

Objective-C type encoding creation and parsing in Rust
Rust
9
star
9

rust-objc-exception

Rust interface for Objective-C's throw and try/catch statements.
Rust
6
star
10

malloc_buf

Structs for handling malloc'd memory passed to Rust.
Rust
6
star
11

rust-rope

A rope for efficiently storing and manipulating large amounts of text
Rust
4
star
12

ZuneMinesweeper

A minesweeper clone for the Zune.
C#
3
star
13

MobileTA

An iPad app for managing classrooms and discussions.
Objective-C
3
star
14

Imagister

A simple image manipulation app for Windows Phone.
C#
2
star
15

game-atlas

A Databases course project for tracking a user's game collection and offering them game recommendations.
Python
2
star
16

rust-feeds

Rust
1
star
17

IntegralApproximator

A windows application to graphically illustrate different integral approximation techniques.
C#
1
star
18

alloc

A slab allocator implementation of malloc
C
1
star
19

Chemistry

Enough of a Chemistry framework to name organic molecules.
C#
1
star
20

Messenger

A messenger program, including the server application and client for WP7.
C#
1
star
21

Polaritoid

An XNA game where the polarity of enemies is important.
C#
1
star
22

cdk-orgo

A Java program to name Organic Molecules using the Chemistry Development Kit.
Java
1
star