logo
Expand description

Alternative to GhostCell that provides safe interior mutability via const expressions.

Advantages

  • Simple, with no cell keys
  • Works better with thread local global state.

Disadvantages

  • Const closures/fn pointers don’t exist (yet), so this crate depends on macro magic to sort-of-polyfill them
  • Might not always optimize well (TODO)

Once const contexts support mutable references, this crate will be able to remove the second disadvantage. Additionally, once const function pointers stabilize, this crate will be able to remove the first disadvantage.

Getting Started

use pure_cell::{PureCell, pure_cell};

let mut cell = PureCell::new(15);
pure_cell!(cell, (), |state: u32, _args: ()| {
    state += 1;
});
let got = cell.get();
assert_eq!(*got, 16);
use pure_cell::{PureCell, pure_cell};

let cell = PureCell::new(15);
let amount = 2;
let state = pure_cell!(cell, amount, |state: u32, amount: u32| -> u32 {
    state += amount;
    state
});
assert_eq!(state, 17);

Macros

Main safe mechanism to mutate PureCell via a const expression.

Structs

A cell type that provides interior mutability via “pure” functions.