4 releases

Uses old Rust 2015

0.1.1 Jul 22, 2018
0.1.0 Mar 20, 2016
0.0.2 Sep 3, 2015
0.0.1 May 2, 2015

#171 in macOS and iOS APIs

Download history 57686/week @ 2024-11-17 55721/week @ 2024-11-24 59705/week @ 2024-12-01 56203/week @ 2024-12-08 59870/week @ 2024-12-15 36777/week @ 2024-12-22 46101/week @ 2024-12-29 54685/week @ 2025-01-05 54543/week @ 2025-01-12 54158/week @ 2025-01-19 52304/week @ 2025-01-26 59091/week @ 2025-02-02 67552/week @ 2025-02-09 60342/week @ 2025-02-16 58977/week @ 2025-02-23 51544/week @ 2025-03-02

245,057 downloads per month
Used in 1,129 crates (40 directly)

MIT license

11KB
151 lines

Rust smart pointers for Objective-C reference counting.

To ensure that Objective-C objects are retained and released at the proper times, we can use the Id struct.

To enforce aliasing rules, an Id can be either owned or shared; if it is owned, meaning the Id is the only reference to the object, it can be mutably dereferenced. An owned Id can be downgraded to a ShareId which can be cloned to allow multiple references.

Weak references may be created using the WeakId struct.

use objc::runtime::{Class, Object};
use objc_id::{Id, WeakId};

let cls = Class::get("NSObject").unwrap();
let obj: Id<Object> = unsafe {
    Id::from_retained_ptr(msg_send![cls, new])
};
// obj will be released when it goes out of scope

// share the object so we can clone it
let obj = obj.share();
let another_ref = obj.clone();
// dropping our other reference will decrement the retain count
drop(another_ref);

let weak = WeakId::new(&obj);
assert!(weak.load().is_some());
// After the object is deallocated, our weak pointer returns none
drop(obj);
assert!(weak.load().is_none());

Dependencies

~130KB