| 1 |
Objective-C Runtime bindings and wrapper for Rust. |
| 2 |
|
| 3 |
* Documentation: http://ssheldon.github.io/rust-objc/objc/ |
| 4 |
* Crate: https://crates.io/crates/objc |
| 5 |
|
| 6 |
## Messaging objects |
| 7 |
|
| 8 |
Objective-C objects can be messaged using the `msg_send!` macro: |
| 9 |
|
| 10 |
``` rust |
| 11 |
let cls = class!(NSObject); |
| 12 |
let obj: *mut Object = msg_send![cls, new]; |
| 13 |
let hash: usize = msg_send![obj, hash]; |
| 14 |
let is_kind: BOOL = msg_send![obj, isKindOfClass:cls]; |
| 15 |
// Even void methods must have their return type annotated |
| 16 |
let _: () = msg_send![obj, release]; |
| 17 |
``` |
| 18 |
|
| 19 |
## Reference counting |
| 20 |
|
| 21 |
The utilities of the `rc` module provide ARC-like semantics for working with |
| 22 |
Objective-C's reference counted objects in Rust. |
| 23 |
A `StrongPtr` retains an object and releases the object when dropped. |
| 24 |
A `WeakPtr` will not retain the object, but can be upgraded to a `StrongPtr` |
| 25 |
and safely fails if the object has been deallocated. |
| 26 |
|
| 27 |
``` rust |
| 28 |
// StrongPtr will release the object when dropped |
| 29 |
let obj = unsafe { |
| 30 |
StrongPtr::new(msg_send![class!(NSObject), new]) |
| 31 |
}; |
| 32 |
|
| 33 |
// Cloning retains the object an additional time |
| 34 |
let cloned = obj.clone(); |
| 35 |
autoreleasepool(|| { |
| 36 |
// Autorelease consumes the StrongPtr, but won't |
| 37 |
// actually release until the end of an autoreleasepool |
| 38 |
cloned.autorelease(); |
| 39 |
}); |
| 40 |
|
| 41 |
// Weak references won't retain the object |
| 42 |
let weak = obj.weak(); |
| 43 |
drop(obj); |
| 44 |
assert!(weak.load().is_null()); |
| 45 |
``` |
| 46 |
|
| 47 |
## Declaring classes |
| 48 |
|
| 49 |
Classes can be declared using the `ClassDecl` struct. Instance variables and |
| 50 |
methods can then be added before the class is ultimately registered. |
| 51 |
|
| 52 |
The following example demonstrates declaring a class named `MyNumber` that has |
| 53 |
one ivar, a `u32` named `_number` and a `number` method that returns it: |
| 54 |
|
| 55 |
``` rust |
| 56 |
let superclass = class!(NSObject); |
| 57 |
let mut decl = ClassDecl::new("MyNumber", superclass).unwrap(); |
| 58 |
|
| 59 |
// Add an instance variable |
| 60 |
decl.add_ivar::<u32>("_number"); |
| 61 |
|
| 62 |
// Add an ObjC method for getting the number |
| 63 |
extern fn my_number_get(this: &Object, _cmd: Sel) -> u32 { |
| 64 |
unsafe { *this.get_ivar("_number") } |
| 65 |
} |
| 66 |
unsafe { |
| 67 |
decl.add_method(sel!(number), |
| 68 |
my_number_get as extern fn(&Object, Sel) -> u32); |
| 69 |
} |
| 70 |
|
| 71 |
decl.register(); |
| 72 |
``` |
| 73 |
|
| 74 |
## Exceptions |
| 75 |
|
| 76 |
By default, if the `msg_send!` macro causes an exception to be thrown, this |
| 77 |
will unwind into Rust resulting in unsafe, undefined behavior. |
| 78 |
However, this crate has an `"exception"` feature which, when enabled, wraps |
| 79 |
each `msg_send!` in a `@try`/`@catch` and panics if an exception is caught, |
| 80 |
preventing Objective-C from unwinding into Rust. |
| 81 |
|
| 82 |
## Message type verification |
| 83 |
|
| 84 |
The Objective-C runtime includes encodings for each method that describe the |
| 85 |
argument and return types. This crate can take advantage of these encodings to |
| 86 |
verify that the types used in Rust match the types encoded for the method. |
| 87 |
|
| 88 |
To use this functionality, enable the `"verify_message"` feature. |
| 89 |
With this feature enabled, type checking is performed for every message send, |
| 90 |
which also requires that all arguments and return values for all messages |
| 91 |
implement `Encode`. |
| 92 |
|
| 93 |
If this requirement is burdensome or you'd rather just verify specific messages, |
| 94 |
you can call the `Message::verify_message` method for specific selectors. |
| 95 |
|
| 96 |
## Support for other Operating Systems |
| 97 |
|
| 98 |
The bindings can be used on Linux or *BSD utilizing the |
| 99 |
[GNUstep Objective-C runtime](https://www.github.com/gnustep/libobjc2). |