- Notifications
You must be signed in to change notification settings - Fork60
Objective-C Runtime bindings and wrapper for Rust.
License
SSheldon/rust-objc
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Objective-C Runtime bindings and wrapper for Rust.
- Documentation:http://ssheldon.github.io/rust-objc/objc/
- Crate:https://crates.io/crates/objc
Objective-C objects can be messaged using themsg_send!
macro:
let cls =class!(NSObject);let obj:*mutObject =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 annotatedlet _:() =msg_send![obj, release];
The utilities of therc
module provide ARC-like semantics for working withObjective-C's reference counted objects in Rust.AStrongPtr
retains an object and releases the object when dropped.AWeakPtr
will not retain the object, but can be upgraded to aStrongPtr
and safely fails if the object has been deallocated.
// StrongPtr will release the object when droppedlet obj =unsafe{StrongPtr::new(msg_send![class!(NSObject), new])};// Cloning retains the object an additional timelet 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 objectlet weak = obj.weak();drop(obj);assert!(weak.load().is_null());
Classes can be declared using theClassDecl
struct. Instance variables andmethods can then be added before the class is ultimately registered.
The following example demonstrates declaring a class namedMyNumber
that hasone ivar, au32
named_number
and anumber
method that returns it:
let superclass =class!(NSObject);letmut decl =ClassDecl::new("MyNumber", superclass).unwrap();// Add an instance variabledecl.add_ivar::<u32>("_number");// Add an ObjC method for getting the numberexternfnmy_number_get(this:&Object,_cmd:Sel) ->u32{unsafe{*this.get_ivar("_number")}}unsafe{ decl.add_method(sel!(number), my_number_getasexternfn(&Object,Sel) ->u32);}decl.register();
By default, if themsg_send!
macro causes an exception to be thrown, thiswill unwind into Rust resulting in unsafe, undefined behavior.However, this crate has an"exception"
feature which, when enabled, wrapseachmsg_send!
in a@try
/@catch
and panics if an exception is caught,preventing Objective-C from unwinding into Rust.
The Objective-C runtime includes encodings for each method that describe theargument and return types. This crate can take advantage of these encodings toverify 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 messagesimplementEncode
.
If this requirement is burdensome or you'd rather just verify specific messages,you can call theMessage::verify_message
method for specific selectors.
The bindings can be used on Linux or *BSD utilizing theGNUstep Objective-C runtime.
About
Objective-C Runtime bindings and wrapper for Rust.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.