1 个不稳定版本
0.1.0 | 2022年10月20日 |
---|
#18 在 #ext
9KB
196 代码行(不含注释)
arc-ext
Arc
使用方法
ArcExt
特性实现扩展了 Arc<T>
,添加了 .project
和 .project_option
方法。
投影强制执行生命周期,因此无法有引用比投影存在的时间更长(因此不是不安全的)。
见以下示例
use arc_ext::ArcExt;
#[derive(Debug, PartialEq, Eq)]
struct Top {
nested: Nested,
string: String,
}
#[derive(Debug, PartialEq, Eq)]
struct Nested {
a: u32,
b: Box<[u8]>,
c: Option<Arc<Top>>,
}
fn test() {
let top = Arc::new(Top {
nested: Nested {
a: 32,
b: vec![1, 2, 3, 4].into_boxed_slice(),
c: Some(Arc::new(Top {
nested: Nested {
a: 12,
b: vec![99].into_boxed_slice(),
c: None,
},
string: "nested".to_string(),
})),
},
string: "owned str".to_string(),
});
let project = top.clone().project(|x| &x.nested.b);
assert_eq!(&[1, 2, 3, 4], &**project);
drop(project);
let project = top.clone().project_option(|x| x.nested.c.as_ref());
let opt = project.as_option().unwrap();
assert_eq!(top.nested.c.as_ref().unwrap(), opt);
}
许可证
此项目采用以下任一许可证:
- Apache 许可证2.0版本 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
依赖
~0–11MB
~130K SLoC