pub struct AsyncLocker<K, V = ()> { /* private fields */ }
Implementations§
Source§impl<K: Eq + Hash, V> AsyncLocker<K, V>
impl<K: Eq + Hash, V> AsyncLocker<K, V>
pub fn new_custom( default_mutex_func: impl Fn() -> V + Send + Sync + 'static, ) -> Self
Sourcepub async fn get_mutex(&self, key: K) -> Arc<Mutex<V>>
pub async fn get_mutex(&self, key: K) -> Arc<Mutex<V>>
Return reference to existig Mutex
or insert new one.
Locks the current task until it is able to return Mutex
.
§Examples
ⓘ
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§
Source§impl<K: Clone, V: Clone> Clone for AsyncLocker<K, V>
impl<K: Clone, V: Clone> Clone for AsyncLocker<K, V>
Source§fn clone(&self) -> AsyncLocker<K, V>
fn clone(&self) -> AsyncLocker<K, V>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<K, V> Freeze for AsyncLocker<K, V>
impl<K, V = ()> !RefUnwindSafe for AsyncLocker<K, V>
impl<K, V> Send for AsyncLocker<K, V>
impl<K, V> Sync for AsyncLocker<K, V>
impl<K, V> Unpin for AsyncLocker<K, V>
impl<K, V = ()> !UnwindSafe for AsyncLocker<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more