#cell #reference #refcell #pointers

no-std stack-cell-ref

Share a reference in thread inner

2 unstable releases

0.3.0 Jan 26, 2025
0.2.1 Jan 2, 2025
0.2.0 Jan 2, 2025
0.1.0 May 20, 2024

#789 in Algorithms

Download history 3/week @ 2024-12-08 1/week @ 2024-12-15 218/week @ 2024-12-29 44/week @ 2025-01-05 4/week @ 2025-01-12 105/week @ 2025-01-26 9/week @ 2025-02-02

118 downloads per month

MIT/Apache

7KB
62 lines

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);

lib.rs:

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);

No runtime deps