2 个版本
0.1.1 | 2024 年 4 月 14 日 |
---|---|
0.1.0 | 2024 年 4 月 14 日 |
#765 在 数据结构
376 次每月下载
用于 connector_arrow
6KB
父和子单元
由父单元创建的单元和子单元,通过可变借用父单元创建。当父单元在单元中时,不能以任何方式访问。提供对子单元的可变访问。
这在需要一起存储和移动父单元及其子单元的罕见情况下很有用。
示例
基本用法
struct Hello {
world: i64,
}
let hello = Hello { world: 10 };
let mut pac = pac_cell::PacCell::new(hello, |h| &mut h.world);
let initial = pac.with_mut(|world| {
let i = **world;
**world = 12;
i
});
assert_eq!(initial, 10);
let hello_again = pac.unwrap();
assert_eq!(hello_again.world, 12);
对于类似真实世界的示例,请参阅 crate 测试。
稳定性
此 crate 有不稳定的函数(如 PacCell::new
),当“错误”使用时可能会导致未定义的行为。
请参阅 https://users.rust-lang.org/t/soundness-of-pac-cell-library/108598/4