Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Objective-C Runtime bindings and wrapper for Rust.

License

NotificationsYou must be signed in to change notification settings

SSheldon/rust-objc

Repository files navigation

Objective-C Runtime bindings and wrapper for Rust.

Messaging objects

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];

Reference counting

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 aStrongPtrand 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());

Declaring classes

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();

Exceptions

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.

Message type verification

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.

Support for other Operating Systems

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

Stars

Watchers

Forks

Packages

No packages published

Contributors11


[8]ページ先頭

©2009-2025 Movatter.jp