pub struct MutexGuard<'a, T: ?Sized> { /* private fields */ }Expand description
A borrowed proof of exclusive access to a Mutex.
Mutex::lock and Mutex::try_lock create this guard. It dereferences to the protected
value and releases the lock when dropped.
Implementations§
Source§impl<'a, T: ?Sized> MutexGuard<'a, T>
impl<'a, T: ?Sized> MutexGuard<'a, T>
Sourcepub fn map<U, F>(orig: Self, f: F) -> MappedMutexGuard<'a, U>
pub fn map<U, F>(orig: Self, f: F) -> MappedMutexGuard<'a, U>
Projects this guard to a mutable component of the protected value.
The returned guard keeps the same mutex locked. Call this as
MutexGuard::map(...) so a method named map on T remains accessible through deref.
§Examples
use asyncband::mutex::Mutex;
use asyncband::mutex::MutexGuard;
#[derive(Debug)]
struct User {
id: u32,
profile: UserProfile,
}
#[derive(Debug)]
struct UserProfile {
email: String,
name: String,
}
let user = User {
id: 1,
profile: UserProfile {
email: "user@example.com".to_owned(),
name: "Alice".to_owned(),
},
};
let mutex = Mutex::new(user);
let guard = mutex.lock().await;
// Map to only access the user's profile, allowing fine-grained locking
let profile_guard = MutexGuard::map(guard, |user| &mut user.profile);
assert_eq!(profile_guard.email, "user@example.com");Sourcepub fn filter_map<U, F>(
orig: Self,
f: F,
) -> Result<MappedMutexGuard<'a, U>, Self>
pub fn filter_map<U, F>( orig: Self, f: F, ) -> Result<MappedMutexGuard<'a, 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
MutexGuard::filter_map(...) so a method with the same name on T remains accessible.
§Examples
use asyncband::mutex::Mutex;
use asyncband::mutex::MutexGuard;
#[derive(Debug)]
struct Database {
users: std::collections::HashMap<u32, String>,
admin_user_id: Option<u32>,
}
let mut db = Database {
users: std::collections::HashMap::new(),
admin_user_id: Some(1),
};
db.users.insert(1, "admin@example.com".to_owned());
let mutex = Mutex::new(db);
let guard = mutex.lock().await;
// Try to map to admin user's email if admin exists
let admin_email_guard = MutexGuard::filter_map(guard, |db| {
if let Some(admin_id) = db.admin_user_id {
db.users.get_mut(&admin_id)
} else {
None
}
})
.expect("admin user should exist");
assert_eq!(&*admin_email_guard, "admin@example.com");Trait Implementations§
Source§impl<T: ?Sized> Deref for MutexGuard<'_, T>
impl<T: ?Sized> Deref for MutexGuard<'_, T>
Source§impl<T: ?Sized> DerefMut for MutexGuard<'_, T>
impl<T: ?Sized> DerefMut for MutexGuard<'_, T>
Source§impl<T: ?Sized> Drop for MutexGuard<'_, T>
impl<T: ?Sized> Drop for MutexGuard<'_, T>
impl<T: ?Sized + Send + Sync> Sync for MutexGuard<'_, T>
Auto Trait Implementations§
impl<'a, T> !RefUnwindSafe for MutexGuard<'a, T>
impl<'a, T> !UnwindSafe for MutexGuard<'a, T>
impl<'a, T> Freeze for MutexGuard<'a, T>
impl<'a, T> Send for MutexGuard<'a, T>
impl<'a, T> Unpin for MutexGuard<'a, T>
impl<'a, T> UnsafeUnpin for MutexGuard<'a, 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