Crates.io | stack-cell-ref |
lib.rs | stack-cell-ref |
version | 0.1.0 |
source | src |
created_at | 2024-05-20 05:15:49.364639 |
updated_at | 2024-05-20 05:15:49.364639 |
description | Share a reference in thread inner. |
homepage | |
repository | https://github.com/823984418/stack-cell-ref |
max_upload_size | |
id | 1245444 |
size | 5,635 |
Share a reference in thread inner.
use std::cell::Cell;
use stack_cell_ref::CellRef;
struct Foo {
x: Cell<i32>,
}
thread_local! {
static S: CellRef<Foo> = CellRef::new();
}
fn set_foo(x: i32) {
S.with(|c| {
c.read(|f| {
f.unwrap().x.set(x);
});
});
}
let foo = Foo { x: Cell::new(0) };
S.with(|c| {
c.with(&foo, || {
set_foo(1);
});
assert_eq!(c.get_ptr(), None);
});
assert_eq!(foo.x.get(), 1);