Struct tokenlock::SingletonToken[][src]

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

A singleton unforgeable token used to access the contents of a TokenLock.

It's a singleton object, meaning there can be only one instance of SingletonToken<Tag> for each specific Tag. The instances of SingletonToken are solely distinguished by its type parameter Tag, making this type zero-sized.

This type lacks a Clone implementation to ensure exclusive access to TokenLock.

This type is invariant over Tag.

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

Sync-ness Conversion

SingletonToken can be converted back and forth to its non-Sync variation, UnsyncSingletonToken, by the following methods:

To SyncTo !Sync
Sync&mutborrow_mutborrow_unsync_mut
&borrow
ownedinto_unsync
!Sync&mutborrow_sync_mutborrow_mut
&borrow_syncborrow
ownedinto_sync

borrow_unsync doesn't exist because &SingletonToken (the Sync variation) might already be owned by multiple threads, so converting them to the !Sync variation would violate the !Sync requirement.

The non-Sync variation can be used to access the contents of UnsyncTokenLock.

Implementations

impl<Tag: ?Sized + SingletonTokenFactory, Variant: SingletonTokenVariant> SingletonToken<Tag, Variant>[src]

pub fn new(
) -> Result<SingletonTokenGuard<Tag, Variant>, SingletonTokenExhaustedError>
[src]

Construct Self, using Tag's SingletonTokenFactory implementation to ensure only one token is present at once.

Returns an RAII guard that derefs to SingletonToken and returns the token when dropped.

Examples

struct MyTag;
impl_singleton_token_factory!(MyTag);

type MyTokenLock<T> = TokenLock<T, SingletonTokenId<MyTag>>;
type MyToken = SingletonToken<MyTag>;
type MyTokenId = SingletonTokenId<MyTag>;

static LOCK1: MyTokenLock<u32> = MyTokenLock::new(MyTokenId::new(), 1);
static LOCK2: MyTokenLock<u32> = MyTokenLock::new(MyTokenId::new(), 1);

// Create a singleton token with a runtime uniqueness check
let mut token = MyToken::new().unwrap();

LOCK1.read(&*token);
LOCK2.write(&mut *token);

The SingletonTokenFactory implementation remembers that a token has been issued, so attempts to issue two tokens of the same tag will fail:

let token = SingletonToken::<MyTag>::new().unwrap();
assert!(SingletonToken::<MyTag>::new().is_err());

impl<Tag: ?Sized, Variant> SingletonToken<Tag, Variant>[src]

pub const unsafe fn new_unchecked() -> Self[src]

Construct Self without checking the singleton invariant.

Safety

Having more than one instance of SingletonToken<Tag> for a particular Tag violates Token's requirements and allows a TokenLock to be mutably borrowed simultaneously, violating the pointer aliasing rules.

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_mut(&mut self) -> SingletonTokenRefMut<'_, Tag, Variant>[src]

Borrow self mutably as SingletonTokenRefMut or UnsyncSingletonTokenRefMut of the same Sync-ness as Self.

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

impl<Tag: ?Sized> SingletonToken<Tag, UnsyncVariant>[src]

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

Borrow self: UnsyncSingletonToken as SingletonTokenRef.

pub fn borrow_sync_mut(&mut self) -> SingletonTokenRefMut<'_, Tag>[src]

Borrow self: UnsyncSingletonToken mutably as SingletonTokenRefMut.

impl<Tag: ?Sized> SingletonToken<Tag>[src]

pub fn borrow_unsync_mut(&mut self) -> UnsyncSingletonTokenRefMut<'_, Tag>[src]

Borrow self: SingletonToken mutably as UnsyncSingletonTokenRefMut.

There is no borrow_unsync because that would allow UnsyncSingletonToken to be logically borrowed by multiple threads.

impl<Tag: ?Sized> SingletonToken<Tag>[src]

pub const fn into_unsync(self) -> UnsyncSingletonToken<Tag>[src]

Convert SingletonToken to the !Sync variant.

impl<Tag: ?Sized> SingletonToken<Tag, UnsyncVariant>[src]

pub const fn into_sync(self) -> SingletonToken<Tag>[src]

Convert UnsyncSingletonToken to the Sync variant.

Trait Implementations

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Debug for SingletonToken<Tag, Variant>[src]

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Eq for SingletonToken<Tag, Variant>[src]

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Hash for SingletonToken<Tag, Variant>[src]

impl<Tag: ?Sized, Variant: SingletonTokenVariant> PartialEq<SingletonToken<Tag, Variant>> for SingletonToken<Tag, Variant>[src]

impl<Tag: ?Sized, Variant: SingletonTokenVariant> Token<SingletonTokenId<Tag>> for SingletonToken<Tag, Variant>[src]

Auto Trait Implementations

impl<Tag: ?Sized, Variant> RefUnwindSafe for SingletonToken<Tag, Variant> where
    Variant: RefUnwindSafe
[src]

impl<Tag: ?Sized, Variant> Send for SingletonToken<Tag, Variant> where
    Variant: Send
[src]

impl<Tag: ?Sized, Variant> Sync for SingletonToken<Tag, Variant> where
    Variant: Sync
[src]

impl<Tag: ?Sized, Variant> Unpin for SingletonToken<Tag, Variant> where
    Variant: Unpin
[src]

impl<Tag: ?Sized, Variant> UnwindSafe for SingletonToken<Tag, Variant> where
    Variant: UnwindSafe
[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.