Type Definition tokenlock::UnsyncSingletonTokenRef[][src]

type UnsyncSingletonTokenRef<'a, Tag> = SingletonTokenRef<'a, Tag, UnsyncVariant>;

Zero-sized logical equivalent of &'a UnsyncSingletonToken<Tag>. The !Sync variant of SingletonTokenRef.

Examples

struct MyTag;
impl_singleton_token_factory!(MyTag);
let token = UnsyncSingletonToken::<MyTag>::new().unwrap();
let lock = UnsyncTokenLock::new(token.id(), 1);

// `UnsyncSingletonTokenRef` is zero-sized (unlike `&UnsyncSingletonToken`), so there is
// no runtime overhead involved with passing `UnsyncSingletonTokenRef`.
access_lock(token.borrow(), &lock);

fn access_lock(
    token: UnsyncSingletonTokenRef<'_, MyTag>,
    lock: &UnsyncTokenLock<u32, SingletonTokenId<MyTag>>,
) {
    assert_eq!(*lock.read(&*token), 1);
}

UnsyncSingletonTokenRef does not allow mutable borrow:

let token_ref: UnsyncSingletonTokenRef<MyTag> = token.borrow();

// compile error: `UnsyncSingletonTokenRef` does not implement `DerefMut`
*lock.write(&mut *token_ref) = 4;

UnsyncSingletonTokenRef is not Send-able because UnsyncSingletonToken is not Sync:

let token: &'static UnsyncSingletonTokenGuard<MyTag> =
    Box::leak(Box::new(UnsyncSingletonToken::<MyTag>::new().unwrap()));
let token_ref: UnsyncSingletonTokenRef<MyTag> = token.borrow();
// compile error: `UnsyncSingletonTokenRef` is not `Send`
std::thread::spawn(move || {
    let _token_ref = token_ref;
});