Crates.io | arc-cell |
lib.rs | arc-cell |
version | 0.3.3 |
source | src |
created_at | 2016-12-10 07:19:23.429011 |
updated_at | 2022-03-23 21:30:09.363881 |
description | Helper for a simple Cell-like object containing Arc/Weak |
homepage | |
repository | https://github.com/Connicpu/arc-cell |
max_upload_size | |
id | 7526 |
size | 10,906 |
A simple library for a concurrent Cell-like object containing an Arc/Weak reference.
[dependencies]
arc-cell = "0.3"
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);
}
}
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")
}
}