pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
An asynchronous mutex that grants exclusive access in request order.
See the module level documentation for more.
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwraps the protected value.
§Examples
use asyncband::mutex::Mutex;
let mutex = Mutex::new(1);
let n = mutex.into_inner();
assert_eq!(n, 1);Source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
Sourcepub async fn lock(&self) -> MutexGuard<'_, T>
pub async fn lock(&self) -> MutexGuard<'_, T>
Sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Acquires the mutex without waiting, or returns None when it is already held.
§Examples
use asyncband::mutex::Mutex;
let mutex = Mutex::new(1);
let mut guard = mutex.try_lock().expect("mutex is locked");
*guard += 1;
assert_eq!(2, *guard);Sourcepub async fn lock_owned(self: Arc<Self>) -> OwnedMutexGuard<T>
pub async fn lock_owned(self: Arc<Self>) -> OwnedMutexGuard<T>
Waits for exclusive access and returns a guard that owns this Arc.
The owned guard keeps the mutex alive instead of borrowing it, which allows the guard to be
moved wherever a 'static value is required.
§Cancel safety
Pending lock requests complete in order. Cancelling this call loses its place among them.
§Examples
use std::sync::Arc;
use asyncband::mutex::Mutex;
let mutex = Arc::new(Mutex::new(1));
let mut n = mutex.clone().lock_owned().await;
*n = 2;Sourcepub fn try_lock_owned(self: Arc<Self>) -> Option<OwnedMutexGuard<T>>
pub fn try_lock_owned(self: Arc<Self>) -> Option<OwnedMutexGuard<T>>
Acquires the mutex without waiting and returns a guard that owns this Arc.
Returns None when another guard currently holds the mutex.
§Examples
use std::sync::Arc;
use asyncband::mutex::Mutex;
let mutex = Arc::new(Mutex::new(1));
let mut guard = mutex.clone().try_lock_owned().expect("mutex is locked");
*guard += 1;
assert_eq!(2, *guard);Trait Implementations§
impl<T: ?Sized + Send> Send for Mutex<T>
impl<T: ?Sized + Send> Sync for Mutex<T>
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T>
impl<T> UnsafeUnpin for Mutex<T>
impl<T> UnwindSafe for Mutex<T>
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