cell-project
A safe interface to project through shared references to core::cell::Cell
.
Documentation: https://docs.rs/cell-project
use Cell;
use cell_project as cp; // renamed for ergonomics
The syntax for the macro is as follows
let projection = cp!;
You may not pass an expression for $value_identifier
, if you need to then you should do.
let value = new;
let projection = cp!;
If you need to project through multiple fields then you need to call cp!
multiple times, once per projection
;
// let some_pair: &Cell<Pair<Point>>;
let point = cp!;
let x = cp!;
note: for generic types, you can use _
to infer the generic parameters
Some limitations, you cannot project an enum variant because that is potentially unsound.
let x = new;
// let's imagine a macro like `try_cell_project`, which takes a varaint as well as a type
let proj = try_cell_project!.unwrap;
x.set; // we can still write to the `Cell` directly
// this will read uninitialized memory (because that's what `None` wrote in)
// and there is no way to fix this. Enums cannot allow safe projection through
// a shared mutable reference (like `&Cell<_>`)
let _ = proj.get;
so you cannot project through enums
Another limitation of stable, you can only project to Sized
types. For example, if I have a type
;
Then I can only project to the first field, because the second field is !Sized
features
nightly
- unlocks cell_project::nightly_cell_project
, which uses the unstable #![feature(raw_ref_op)]
to
allow projections to !Sized
fields.
License: MIT