pub struct MappedMutexGuard<'a, T: ?Sized> { /* private fields */ }Expand description
A borrowed mutex guard projected to a mutable component of the protected value.
MutexGuard::map and MutexGuard::filter_map create this guard. It keeps the mutex locked
while exposing only the projected component.
§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;
let profile_guard = MutexGuard::map(guard, |user| &mut user.profile);
// Now we can only access the user's profile
assert_eq!(profile_guard.email, "user@example.com");Implementations§
Source§impl<'a, T: ?Sized> MappedMutexGuard<'a, T>
impl<'a, T: ?Sized> MappedMutexGuard<'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 an already mapped guard to a deeper mutable component.
The returned guard keeps the same mutex locked. Call this as
MappedMutexGuard::map(...) so a method named map on T remains accessible through
deref.
§Examples
use asyncband::mutex::MappedMutexGuard;
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;
// First map to user profile
let profile_guard = MutexGuard::map(guard, |user| &mut user.profile);
// Then map to the email field specifically
let email_guard = MappedMutexGuard::map(profile_guard, |profile| &mut profile.email);
assert_eq!(&*email_guard, "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 an already mapped guard to a deeper mutable component.
The original mapped guard is returned when f returns None. Call this as
MappedMutexGuard::filter_map(...) so a method with the same name on T remains
accessible.
§Examples
use asyncband::mutex::MappedMutexGuard;
use asyncband::mutex::Mutex;
use asyncband::mutex::MutexGuard;
#[derive(Debug)]
struct Data {
id: u32,
value: Option<String>,
}
let data = Data {
id: 1,
value: Some("hello".to_owned()),
};
let mutex = Mutex::new(data);
let guard = mutex.lock().await;
// First map to the value field
let value_guard = MutexGuard::map(guard, |data| &mut data.value);
// Then try to map to the inner string if it exists
let string_guard =
MappedMutexGuard::filter_map(value_guard, |opt| opt.as_mut()).expect("value should exist");
assert_eq!(&*string_guard, "hello");Trait Implementations§
Source§impl<T: ?Sized> Deref for MappedMutexGuard<'_, T>
impl<T: ?Sized> Deref for MappedMutexGuard<'_, T>
Source§impl<T: ?Sized> DerefMut for MappedMutexGuard<'_, T>
impl<T: ?Sized> DerefMut for MappedMutexGuard<'_, T>
Source§impl<T: ?Sized> Drop for MappedMutexGuard<'_, T>
impl<T: ?Sized> Drop for MappedMutexGuard<'_, T>
impl<T: ?Sized + Send> Send for MappedMutexGuard<'_, T>
impl<T: ?Sized + Sync> Sync for MappedMutexGuard<'_, T>
Auto Trait Implementations§
impl<'a, T> !UnwindSafe for MappedMutexGuard<'a, T>
impl<'a, T> Freeze for MappedMutexGuard<'a, T>
impl<'a, T> RefUnwindSafe for MappedMutexGuard<'a, T>
impl<'a, T> Unpin for MappedMutexGuard<'a, T>
impl<'a, T> UnsafeUnpin for MappedMutexGuard<'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