Struct tokenlock::SingletonTokenRef[][src]

pub struct SingletonTokenRef<'a, Tag: ?Sized, Variant = SyncVariant>(_);

Zero-sized logical equivalent of &'a SingletonToken<Tag>.

The second type parameter (Variant) is internal use only and exempt from the API stability guarantee.

Examples

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

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

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

SingletonTokenRef does not allow mutable borrow:

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

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

SingletonTokenRef is Send-able because SingletonToken is Sync:

let token: &'static SingletonTokenGuard<MyTag> =
    Box::leak(Box::new(SingletonToken::<MyTag>::new().unwrap()));
let token_ref: SingletonTokenRef<MyTag> = token.borrow();
std::thread::spawn(move || {
    let _token_ref = token_ref;
});

Methods from Deref<Target = SingletonToken<Tag, Variant>>

pub fn id(&self) -> SingletonTokenId<Tag>[src]

Construct an SingletonTokenId that equates to self.

pub fn borrow(&self) -> SingletonTokenRef<'_, Tag, Variant>[src]

Borrow self as SingletonTokenRef or UnsyncSingletonTokenRef of the same Sync-ness as Self.

SingletonTokenRef is truly zero-sized, so it's more efficient to store and pass than &SingletonToken.

pub fn borrow_sync(&self) -> SingletonTokenRef<'_, Tag>[src]

Borrow self: UnsyncSingletonToken as SingletonTokenRef.

Trait Implementations

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Debug for SingletonTokenRef<'_, Tag, Variant>[src]

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Deref for SingletonTokenRef<'_, Tag, Variant>[src]

type Target = SingletonToken<Tag, Variant>

The resulting type after dereferencing.

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Eq for SingletonTokenRef<'_, Tag, Variant>[src]

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Hash for SingletonTokenRef<'_, Tag, Variant>[src]

impl<Tag: ?Sized, Variant: SingletonTokenVariant> PartialEq<SingletonTokenRef<'_, Tag, Variant>> for SingletonTokenRef<'_, Tag, Variant>[src]

Auto Trait Implementations

impl<'a, Tag: ?Sized, Variant> RefUnwindSafe for SingletonTokenRef<'a, Tag, Variant> where
    Variant: RefUnwindSafe
[src]

impl<'a, Tag: ?Sized, Variant> Send for SingletonTokenRef<'a, Tag, Variant> where
    Variant: Sync
[src]

impl<'a, Tag: ?Sized, Variant> Sync for SingletonTokenRef<'a, Tag, Variant> where
    Variant: Sync
[src]

impl<'a, Tag: ?Sized, Variant> Unpin for SingletonTokenRef<'a, Tag, Variant>[src]

impl<'a, Tag: ?Sized, Variant> UnwindSafe for SingletonTokenRef<'a, Tag, Variant> where
    Variant: RefUnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.