Skip to main content

MappedMutexGuard

Struct MappedMutexGuard 

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

Source

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

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");
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 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 + Debug> Debug for MappedMutexGuard<'_, T>

Source§

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

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

impl<T: ?Sized> Deref for MappedMutexGuard<'_, 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 MappedMutexGuard<'_, T>

Source§

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

Mutably dereferences the value.
Source§

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

Source§

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

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

impl<T: ?Sized> Drop for MappedMutexGuard<'_, 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> Send for MappedMutexGuard<'_, T>

Source§

impl<T: ?Sized + Sync> Sync for MappedMutexGuard<'_, T>

Auto Trait Implementations§

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.