tokenlock
Provides a Send
-able cell type whose contents can be accessed only via an
unforgeable token.
Examples
let mut token = new;
let lock = new;
assert_eq!;
let mut guard = lock.write;
assert_eq!;
*guard = 2;
TokenLock
implements Send
and Sync
so it can be shared between threads,
but only the thread holding the original Token
can access its contents.
Token
cannot be cloned:
let lock = new;
let lock_1 = clone;
new.spawn.unwrap;
// can't access the contents; I no longer have `Token`
// lock.write(&mut token);
The lifetime of the returned reference is limited by both of the TokenLock
and Token
.
let mut token = new;
let lock = new;
let guard = lock.write;
drop; // compile error: `guard` cannot outlive `TokenLock`
drop;
drop; // compile error: `guard` cannot outlive `Token`
drop;
It also prevents from forming a reference to the contained value when there already is a mutable reference to it:
let write_guard = lock.write;
let read_guard = lock.read; // compile error
drop;
While allowing multiple immutable references:
let read_guard1 = lock.read;
let read_guard2 = lock.read;
License: MIT/Apache-2.0