pub struct OwnedMutexGuard<T: ?Sized> { /* private fields */ }Expand description
An owned handle to a held Mutex.
This guard is only available from a Mutex that is wrapped in an Arc. It is identical to
MutexGuard, except that rather than borrowing the Mutex, it clones the Arc, incrementing
the reference count. This means that unlike MutexGuard, it will have the 'static lifetime.
As long as you have this guard, you have exclusive access to the underlying T. The guard
internally keeps a reference-counted pointer to the original Mutex, so even if the lock goes
away, the guard remains valid.
The lock is automatically released whenever the guard is dropped, at which point lock will
succeed yet again.
See the module level documentation for more.
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>
Makes a new OwnedMappedMutexGuard for a component of the locked data.
This operation cannot fail as the OwnedMutexGuard passed in already locked the mutex.
This is an associated function that needs to be used as OwnedMutexGuard::map(...). A
method would interfere with methods of the same name on the contents of the locked data.
§Examples
use std::sync::Arc;
use mea::mutex::Mutex;
use mea::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 make a new OwnedMappedMutexGuard for a component of the locked data. The
original guard is returned if the closure returns None.
This operation cannot fail as the OwnedMutexGuard passed in already locked the mutex.
This is an associated function that needs to be used as OwnedMutexGuard::filter_map(...).
A method would interfere with methods of the same name on the contents of the locked data.
§Examples
use std::sync::Arc;
use mea::mutex::Mutex;
use mea::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);