stack-cell-ref

Crates.iostack-cell-ref
lib.rsstack-cell-ref
version0.3.0
created_at2024-05-20 05:15:49.364639+00
updated_at2025-01-26 06:17:05.727375+00
descriptionShare a reference in thread inner.
homepage
repositoryhttps://github.com/823984418/stack-cell-ref
max_upload_size
id1245444
size18,930
(823984418)

documentation

README

Stack Cell Ref

Share a reference in thread inner.

Examples

use std::cell::Cell;
use stack_cell_ref::CellOptionRef;
struct Foo {
    x: Cell<i32>,
}

thread_local! {
    static S: CellOptionRef<Foo> = CellOptionRef::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(Some(&foo), || {
        set_foo(1);
    });
    assert_eq!(c.get_ptr(), None);
});
assert_eq!(foo.x.get(), 1);
Commit count: 4

cargo fmt