#objective-c #osx #ios

objc_id

Rust 对 Objective-C 引用计数的智能指针

4 个版本

使用旧的 Rust 2015

0.1.1 2018 年 7 月 22 日
0.1.0 2016 年 3 月 20 日
0.0.2 2015 年 9 月 3 日
0.0.1 2015 年 5 月 2 日

#125macOS 和 iOS API

Download history 57200/week @ 2023-10-22 55881/week @ 2023-10-29 65381/week @ 2023-11-05 76935/week @ 2023-11-12 60672/week @ 2023-11-19 58618/week @ 2023-11-26 59894/week @ 2023-12-03 57979/week @ 2023-12-10 51671/week @ 2023-12-17 39028/week @ 2023-12-24 51202/week @ 2023-12-31 57664/week @ 2024-01-07 62590/week @ 2024-01-14 62930/week @ 2024-01-21 69784/week @ 2024-01-28 57284/week @ 2024-02-04

257,727 每月下载量
1,229 个 crate (45 directly) 中使用

MIT 许可证

11KB
151 行代码(不包括注释)

Rust 对 Objective-C 引用计数的智能指针。

为确保 Objective-C 对象在适当的时间被保留和释放,我们可以使用 Id 结构体。

为了强制执行别名规则,Id 可以是拥有者或共享者;如果它是拥有者,意味着 Id 是对象的唯一引用,它可以进行可变解引用。拥有的 Id 可以降级为 ShareId,它可以被克隆以允许多个引用。

可以使用 WeakId 结构体创建弱引用。

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

依赖关系

~130KB