Crates.io | objr |
lib.rs | objr |
version | 1.0.0 |
source | src |
created_at | 2024-10-13 19:03:35.504974 |
updated_at | 2024-10-13 19:03:35.504974 |
description | Drew's very fast ObjC Rust bindings |
homepage | https://sealedabstract.com/code/objr |
repository | https://github.com/drewcrawford/objr |
max_upload_size | |
id | 1407594 |
size | 183,790 |
This library provides low-level Rust bindings for ObjC, which is in practice the ABI for macOS. You might compare this crate with objc, fruity, objcrs and many others.
Distinctive features of this library include:
//We intend to write bindings for ObjC APIs
use objr::bindings::*;
objc_class! {
//Rust wrapper type
pub struct NSDate {
//ObjC class name
@class(NSDate)
}
}
autoreleasepool(|pool| {
//In this library, autoreleasepools are often arguments to ObjC-calling APIs, providing static guarantees you created one.
//Forgetting this is a common ObjC bug.
let date = NSDate::class().alloc_init(&pool);
println!("{}",date); // 2021-06-21 19:03:15 +0000
})
Compare this with objc_instance!
for non-class instances.
use objr::bindings::*;
objc_class! {
//Rust wrapper type
pub struct NSDate {
@class(NSDate)
}
}
//Declares a group of static selectors.
objc_selector_group! {
pub trait NSDateSelectors {
@selector("dateByAddingTimeInterval:")
}
//Adds support for these selectors to our `Sel` APIs.
impl NSDateSelectors for Sel {}
}
//Declare APIs directly on our `NSDate` wrapper type
impl NSDate {
fn dateByAddingTimeInterval(&self, pool: &ActiveAutoreleasePool, interval: f64)
//Although the underlying ObjC API returns a +0 unowned reference,
//We create a binding that returns +1 retained instead. We might do this
//because it's the preferred pattern of our application.
//For more details, see the documentation of [objc_instance!]
-> StrongCell<NSDate> {
//Use of ObjC is unsafe. There is no runtime or dynamic checking of your work here,
//so you must provide a safe abstraction to callers (or mark the enclosing function unsafe).
unsafe {
/*Convert from an autoreleased return value to a strong one.
This uses tricks used by real ObjC compilers and is far faster than calling `retain` yourself.
*/
let raw = Self::perform_autorelease_to_retain(
//the objc method we are calling does not mutate the receiver
self.assume_nonmut_perform(),
///Use the compile-time selector we declared above
Sel::dateByAddingTimeInterval_(),
///Static checking that we have an autoreleasepool available
pool,
///Arguments. Note the trailing `,`. Arguments are tuple types.
(interval,));
//assume the result is nonnil
Self::assume_nonnil(raw)
//assume the object is +1 convention (it is, because we called perform_autorelease_to_retain above)
.assume_retained()
}
}
}
autoreleasepool(|pool| {
//In this library, autoreleasepools are often arguments to ObjC-calling APIs, providing compile-time guarantees you created one.
//Forgetting this is a common ObjC bug.
let date = NSDate::class().alloc_init(&pool);
let new_date = date.dateByAddingTimeInterval(&pool, 23.5);
})
For more examples, see the documentation for objc_instance!
.
retain
/release
/autorelease
calls in many cases.Option<&NSObject>
StrongCell
and AutoreleasedCell
Not yet implemented, but planned or possible:
This library intends to follow normal guidelines for safe Rust. However, calling into ObjC means there's
a giant ball of unsafe
somewhere.
This library makes the assumption that the underlying ball of ObjC is implemented correctly. In particular, it avoids runtime checks that ObjC is implemented correctly, so to the extent that it isn't, you may encounter UB.
For more information, see the safety section of objc_instance
!
objr is part of an expanded universe of related crates that take a similar approach to binding Apple technologies to Rust.