Skip to main content

MutexGuard

Struct MutexGuard 

Source
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>

Source

pub fn map<U, F>(orig: Self, f: F) -> MappedMutexGuard<'a, U>
where F: FnOnce(&mut T) -> &mut U, U: ?Sized,

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");
Source

pub fn filter_map<U, F>( orig: Self, f: F, ) -> Result<MappedMutexGuard<'a, U>, Self>
where F: FnOnce(&mut T) -> Option<&mut U>, U: ?Sized,

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 + Debug> Debug for MutexGuard<'_, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Deref for MutexGuard<'_, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: ?Sized> DerefMut for MutexGuard<'_, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: ?Sized + Display> Display for MutexGuard<'_, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Drop for MutexGuard<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

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>
where &'a Mutex<T>: Freeze, T: ?Sized,

§

impl<'a, T> Send for MutexGuard<'a, T>
where &'a Mutex<T>: Send, T: ?Sized,

§

impl<'a, T> Unpin for MutexGuard<'a, T>
where &'a Mutex<T>: Unpin, T: ?Sized,

§

impl<'a, T> UnsafeUnpin for MutexGuard<'a, T>
where &'a Mutex<T>: UnsafeUnpin, T: ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.