Crate tokenlock [] [src]

Cell type whose contents can be accessed only via an inforgeable token.

Examples

let mut token = Token::new();

let lock = TokenLock::new(&token, 1);
assert_eq!(*lock.read(&token).unwrap(), 1);

let mut guard = lock.write(&mut token).unwrap();
assert_eq!(*guard, 1);
*guard = 2;

The lifetime of the returned reference is limited by both of the TokenLock and Token.

This code doesn't compile so be extra careful!
drop(lock); // compile error: cannot outlive `TokenLock`
This code doesn't compile so be extra careful!
drop(token); // compile error: cannot outlive `Token`

This also prevents from forming a reference to the contained value when there already is a mutable reference to it:

This code doesn't compile so be extra careful!
let write_guard = lock.write(&mut token).unwrap();
let read_guard = lock.read(&token).unwrap(); // compile error

While allowing multiple immutable references:

let read_guard1 = lock.read(&token).unwrap();
let read_guard2 = lock.read(&token).unwrap();

Structs

Token

An inforgeable token used to access the contents of a TokenLock.

TokenLock

A mutual exclusive primitive that can be accessed using a Token with a very low over-head.

TokenRef

Token that cannot be used to access the contents of a TokenLock, but can be used to create a new TokenLock.

UniqueId