Expand description
§arc-cell
A simple library for a concurrent Cell-like object containing an Arc/Weak reference.
[dependencies]
arc-cell = "0.3"
§Usage
§Lightweight swappable Arc member
use std::sync::Arc;
use arc_cell::ArcCell;
pub struct Thing {
data: ArcCell<Vec<u8>>,
}
impl Thing {
pub fn update(&self, data: Arc<Vec<u8>>) {
self.data.set(data);
}
}
§Self-referencial structure
use std::sync::Arc;
use arc_cell::WeakCell;
pub struct Thing {
self_ref: WeakCell<Thing>,
// ...
}
impl Thing {
pub fn new() -> Arc<Thing> {
let thing = Arc::new(Thing {
self_ref: WeakCell::empty(),
});
thing.self_ref.store(&thing);
thing
}
pub fn clone_ref(&self) -> Arc<Thing> {
self.self_ref.upgrade().expect("This should be valid if we have a valid self")
}
}
Structs§
- Atomic
Cell - An atomic-based cell designed for holding Arc-style pointers.
Traits§
- Atomic
Cell Const Init - Atomic
Cell Storable - It is up to the implementer to ensure this is safe to implement.
Type Aliases§
- ArcCell
- Atomically swappable/clonable Arc pointer value.
- Optional
ArcCell - Atomically swappable/clonable/optional Arc pointer value.
- Optional
Weak Cell - Atomically swappable/clonable/optional Weak Arc pointer value.
- Weak
Cell - Atomically swappable/clonable Weak Arc pointer value.