Struct ghost_cell::ghost_cell::GhostToken
source · [−]pub struct GhostToken<'brand> { /* private fields */ }
Expand description
A GhostToken<'x>
is the key to access the content of any &GhostCell<'x, _>
sharing the same brand.
Each GhostToken<'x>
is created alongside a unique brand (its lifetime), and each GhostCell<'x, T>
is associated
to one, and only one, GhostToken
at a time via this brand. The entire set of GhostCell<'x, T>
associated to a
given GhostToken<'x>
creates a pool of cells all being accessible solely through the one token they are associated
to.
The pool of GhostCell
associated to a token need not be homogeneous, each may own a value of a different type.
Implementations
sourceimpl<'brand> GhostToken<'brand>
impl<'brand> GhostToken<'brand>
sourcepub fn new<R, F>(fun: F) -> Rwhere
for<'new_brand> F: FnOnce(GhostToken<'new_brand>) -> R,
pub fn new<R, F>(fun: F) -> Rwhere
for<'new_brand> F: FnOnce(GhostToken<'new_brand>) -> R,
Creates a fresh token to which GhostCell
s can be tied to later.
Due to the use of a lifetime, the GhostCell
s tied to a given token can only live within the confines of the
invocation of the fun
closure.
Example
use ghost_cell::{GhostToken, GhostCell};
let n = 12;
let value = GhostToken::new(|mut token| {
let cell = GhostCell::new(42);
let vec: Vec<_> = (0..n).map(|_| &cell).collect();
*vec[n / 2].borrow_mut(&mut token) = 33;
*cell.borrow(&token)
});
assert_eq!(33, value);
Trait Implementations
impl<'brand> Send for GhostToken<'brand>
A GhostToken
is stateless, therefore it can safely be passed across threads.
impl<'brand> Sync for GhostToken<'brand>
A GhostToken
is stateless, therefore it can safely be accessed from different threads.