[][src]Struct locker::AsyncLocker

pub struct AsyncLocker<K, V = ()> { /* fields omitted */ }

Implementations

impl<K: Eq + Hash, V> AsyncLocker<K, V>[src]

pub fn new_custom(
    default_mutex_func: impl Fn() -> V + Send + Sync + 'static
) -> Self
[src]

pub async fn get_mutex<'_>(&'_ self, key: K) -> Arc<Mutex<V>>[src]

Return reference to existig Mutex or insert new one.

Locks the current task until it is able to return Mutex.

Examples

This example is not tested
use std::time::Duration;
use locker::AsyncLocker;
use tokio::time::delay_for;

let default_mutex_value = "value";
let locker = AsyncLocker::<i32, &str>::new_custom(move || default_mutex_value);
let mutex = locker.get_mutex(1).await;
let _guard = mutex.lock().await; // lock
let locker_clone = locker.clone();
tokio::spawn(async move {
    let mutex = locker.get_mutex(1).await;
    let value = mutex.lock().await; // wait
    assert_eq!(default_mutex_value, *value);
});
delay_for(Duration::from_millis(200)).await;

Trait Implementations

impl<K: Clone, V: Clone> Clone for AsyncLocker<K, V>[src]

impl<K: Eq + Hash, V: Default> Default for AsyncLocker<K, V>[src]

Auto Trait Implementations

impl<K, V = ()> !RefUnwindSafe for AsyncLocker<K, V>

impl<K, V> Send for AsyncLocker<K, V> where
    K: Send + Sync,
    V: Send

impl<K, V> Sync for AsyncLocker<K, V> where
    K: Send + Sync,
    V: Send

impl<K, V> Unpin for AsyncLocker<K, V>

impl<K, V = ()> !UnwindSafe for AsyncLocker<K, V>

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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.