Function singleton_cell::with_token
source · [−]Expand description
Generate a local token type which is guaranteed to be a singleton via a unique lifetime brand. SCells can be created within this scope via SCell::new or SCell::from_mut and used via this token.
Because the ’brand lifetime is unique to this scope, neither this token, nor any SCell’s created with it as the key may escape the scope.
It is most likely useful when cells are created with SCell::from_mut, which means that cells can be used for data structures that don’t mention SCell or the ’brand.
For a token type which does not have this restriction, see new_singleton for one implementation.
use singleton_cell::*;
let mut x = 0;
with_token(|mut tok| {
let cell_borrow = &*SCell::from_mut(&mut x);
*cell_borrow.borrow_mut(&mut tok) += 2;
let cell_one = SCell::new(0);
*cell_one.borrow_mut(&mut tok) += 1;
cell_one.into_inner()
});
assert_eq!(2, x);