• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H03-May-2022-4632

src/H03-May-2022-2,5321,755

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

CHANGELOG.mdH A D16-Oct-20193.6 KiB11875

Cargo.lockH A D01-Jan-19701.5 KiB4236

Cargo.tomlH A D01-Jan-19701.1 KiB3430

Cargo.toml.orig-cargoH A D16-Oct-2019630 3226

README.mdH A D16-Oct-20193.2 KiB10075

README.md

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