Function singleton_cell::with_token [−][src]
pub fn with_token<R>(f: impl for<'brand> FnOnce(Token<'brand>) -> R) -> R
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.
This token and all borrows and cells based off of it cannot escape the scope.
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);