pub struct GhostToken<'brand>(/* private fields */);
Expand description
A lifetime based token, inspired by ghost_cell
.
Correct usage, where cells are only unlocked by providing a mutable reference to the token they were constructed with, will compile.
GhostToken::with_token(|mut t1| {
let c1 = TokenCell::new(1, &t1);
GhostToken::with_token(|mut t2| {
let c2 = TokenCell::new(1, &t2);
println!("{}", *c2.borrow_mut(&mut t2));
println!("{}", *c1.borrow_mut(&mut t1));
})
.unwrap();
c1.borrow_mut(&mut t1);
})
.unwrap();
But using the wrong token with any given cell will fail at compile time.
ⓘ
GhostToken::with_token(|mut t1| {
let c1 = TokenCell::new(1, &t1);
GhostToken::with_token(|mut t2| {
println!("{}", *c1.borrow_mut(&mut t2));
})
.unwrap();
c1.borrow_mut(&mut t1);
})
.unwrap();
ⓘ
GhostToken::with_token(|mut t1| {
let c1 = TokenCell::new(1, &t1);
GhostToken::with_token(|mut t2| {
let c2 = TokenCell::new(1, &t2);
println!("{}", *c2.borrow_mut(&mut t1));
println!("{}", *c1.borrow_mut(&mut t1));
})
.unwrap();
c1.borrow_mut(&mut t1);
})
.unwrap();
Trait Implementations§
Source§impl<'brand> TokenTrait for GhostToken<'brand>
impl<'brand> TokenTrait for GhostToken<'brand>
Source§fn with_token<R, F: for<'a> FnOnce(Self::Branded<'a>) -> R>(
f: F,
) -> Result<R, Self::RunError>
fn with_token<R, F: for<'a> FnOnce(Self::Branded<'a>) -> R>( f: F, ) -> Result<R, Self::RunError>
use token_cell::{ghost::*, prelude::*};
GhostToken::with_token(|mut t1| {
let c1 = TokenCell::new(1, &t1);
GhostToken::with_token(|mut t2| {
let c2 = TokenCell::new(1, &t2);
println!("{}", *c2.borrow_mut(&mut t2));
println!("{}", *c1.borrow_mut(&mut t1));
})
.unwrap();
c1.borrow_mut(&mut t1);
})
.unwrap();
ⓘ
use token_cell::{ghost::*, prelude::*};
GhostToken::with_token(|mut t1| {
let c1 = TokenCell::new(1, &t1);
GhostToken::with_token(|mut t2| {
let c2 = TokenCell::new(1, &t2);
println!("{}", *c2.borrow_mut(&mut t2));
println!("{}", *c1.borrow_mut(&mut t2));
})
.unwrap();
c1.borrow_mut(&mut t1);
})
.unwrap();
Source§type ConstructionError = ()
type ConstructionError = ()
Constructing a token may fail.
Source§type RunError = Infallible
type RunError = Infallible
TokenTrait::with_token
may fail, typically if construction failed. Read moreSource§type Identifier = InvariantLifetime<'brand>
type Identifier = InvariantLifetime<'brand>
Source§type ComparisonError = Infallible
type ComparisonError = Infallible
core::convert::Infallible
unless TokenTrait::compare
is fallible (ie. comparison is done at runtime).Source§type Branded<'a> = GhostToken<'a>
type Branded<'a> = GhostToken<'a>
Rebrands the token, this is necessary for
GhostToken
to function properlySource§fn identifier(&self) -> InvariantLifetime<'brand>
fn identifier(&self) -> InvariantLifetime<'brand>
Returns the Token’s identifier, which cells may store to allow comparison.
Source§fn compare(
&self,
_: &InvariantLifetime<'brand>,
) -> Result<(), Self::ComparisonError>
fn compare( &self, _: &InvariantLifetime<'brand>, ) -> Result<(), Self::ComparisonError>
Allows the cell to compare its identifier to the Token. Read more
Auto Trait Implementations§
impl<'brand> Freeze for GhostToken<'brand>
impl<'brand> !RefUnwindSafe for GhostToken<'brand>
impl<'brand> Send for GhostToken<'brand>
impl<'brand> !Sync for GhostToken<'brand>
impl<'brand> Unpin for GhostToken<'brand>
impl<'brand> UnwindSafe for GhostToken<'brand>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more