pub struct OwnedMutexGuard<T: ?Sized> { /* private fields */ }Expand description
A proof of exclusive access that owns an Arc containing its Mutex.
Mutex::lock_owned and Mutex::try_lock_owned create this guard. Owning the Arc lets the
guard outlive the reference used to acquire it; dropping the guard releases the lock and its
share of the Arc.
Implementations§
Source§impl<T: ?Sized> OwnedMutexGuard<T>
impl<T: ?Sized> OwnedMutexGuard<T>
Sourcepub fn map<U, F>(orig: Self, f: F) -> OwnedMappedMutexGuard<T, U>
pub fn map<U, F>(orig: Self, f: F) -> OwnedMappedMutexGuard<T, U>
Projects this guard to a mutable component of the protected value.
The returned guard retains the same Arc and keeps the mutex locked. Call this as
OwnedMutexGuard::map(...) so a method named map on T remains accessible through deref.
§Examples
use std::sync::Arc;
use asyncband::mutex::Mutex;
use asyncband::mutex::OwnedMutexGuard;
struct Config {
name: String,
value: u32,
}
let config = Config {
name: "front size".to_owned(),
value: 42,
};
let mutex = Arc::new(Mutex::new(config));
let guard = mutex.clone().lock_owned().await;
// Map to access only the value field
let value_guard = OwnedMutexGuard::map(guard, |config| &mut config.value);
assert_eq!(*value_guard, 42);Sourcepub fn filter_map<U, F>(
orig: Self,
f: F,
) -> Result<OwnedMappedMutexGuard<T, U>, Self>
pub fn filter_map<U, F>( orig: Self, f: F, ) -> Result<OwnedMappedMutexGuard<T, U>, Self>
Attempts to project this guard to a mutable component of the protected value.
The original guard is returned when f returns None. Call this as
OwnedMutexGuard::filter_map(...) so a method with the same name on T remains accessible.
§Examples
use std::sync::Arc;
use asyncband::mutex::Mutex;
use asyncband::mutex::OwnedMutexGuard;
let data = vec![1, 2, 3, 4, 5];
let mutex = Arc::new(Mutex::new(data));
let guard = mutex.clone().lock_owned().await;
// Map to the first element
let first_guard =
OwnedMutexGuard::filter_map(guard, |vec| vec.get_mut(0)).expect("vec should not be empty");
assert_eq!(*first_guard, 1);Trait Implementations§
Source§impl<T: ?Sized> Deref for OwnedMutexGuard<T>
impl<T: ?Sized> Deref for OwnedMutexGuard<T>
Source§impl<T: ?Sized> DerefMut for OwnedMutexGuard<T>
impl<T: ?Sized> DerefMut for OwnedMutexGuard<T>
Source§impl<T: ?Sized> Drop for OwnedMutexGuard<T>
impl<T: ?Sized> Drop for OwnedMutexGuard<T>
impl<T: ?Sized + Send + Sync> Sync for OwnedMutexGuard<T>
Auto Trait Implementations§
impl<T> !RefUnwindSafe for OwnedMutexGuard<T>
impl<T> !UnwindSafe for OwnedMutexGuard<T>
impl<T> Freeze for OwnedMutexGuard<T>
impl<T> Send for OwnedMutexGuard<T>
impl<T> Unpin for OwnedMutexGuard<T>
impl<T> UnsafeUnpin for OwnedMutexGuard<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