https://github.com/bbqsrc/arc-ext
Extensions for Arc<T> such as field projection.
https://github.com/bbqsrc/arc-ext
arc projection rust
Last synced: 3 months ago
JSON representation
Extensions for Arc<T> such as field projection.
- Host: GitHub
- URL: https://github.com/bbqsrc/arc-ext
- Owner: bbqsrc
- License: apache-2.0
- Created: 2022-10-20T14:20:22.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-20T15:07:10.000Z (over 3 years ago)
- Last Synced: 2025-01-31T17:11:32.367Z (about 1 year ago)
- Topics: arc, projection, rust
- Language: Rust
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# arc-ext
Extensions for `Arc` such as field projection.
## Usage
The `ArcExt` trait implementation extends `Arc` with `.project` and `.project_option` methods.
The projection enforces lifetimes, so that no reference to it can outlive the projection (and therefore is not unsound).
See the following example:
```rust
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>,
}
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);
}
```
## License
This project is licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or )
* MIT license ([LICENSE-MIT](LICENSE-MIT) or )
at your option.