tokenlock 0.1.1

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

tokenlock

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.

drop(lock); // compile error: cannot outlive `TokenLock`
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:

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();

License: MIT/Apache-2.0