Struct MappedMutexGuard
#[must_use = "if unused the Mutex will immediately unlock"]
pub struct MappedMutexGuard<'a, R: RawMutex, T: ?Sized> { /* private fields */ }
An RAII mutex guard returned by MutexGuard::map, which can point to a
subfield of the protected data.
The main difference between MappedMutexGuard and MutexGuard is that the
former doesn't support temporarily unlocking and re-locking, since that
could introduce soundness issues if the locked object is modified by another
thread.
Implementations
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MappedMutexGuard<'a, R, T>
fn map<U: ?Sized, F>(s: Self, f: F) -> MappedMutexGuard<'a, R, U> where F: FnOnce(&mut T) -> &mut U,Makes a new
MappedMutexGuardfor a component of the locked data.This operation cannot fail as the
MappedMutexGuardpassed in already locked the mutex.This is an associated function that needs to be used as
MappedMutexGuard::map(...). A method would interfere with methods of the same name on the contents of the locked data.fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedMutexGuard<'a, R, U>, Self> where F: FnOnce(&mut T) -> Option<&mut U>,Attempts to make a new
MappedMutexGuardfor a component of the locked data. The original guard is returned if the closure returnsNone.This operation cannot fail as the
MappedMutexGuardpassed in already locked the mutex.This is an associated function that needs to be used as
MappedMutexGuard::try_map(...). A method would interfere with methods of the same name on the contents of the locked data.fn try_map_or_err<U: ?Sized, F, E>(s: Self, f: F) -> Result<MappedMutexGuard<'a, R, U>, (Self, E)> where F: FnOnce(&mut T) -> Result<&mut U, E>,Attempts to make a new
MappedMutexGuardfor a component of the locked data. The original guard is returned alongside arbitrary user data if the closure returnsErr.This operation cannot fail as the
MappedMutexGuardpassed in already locked the mutex.This is an associated function that needs to be used as
MappedMutexGuard::try_map_or_err(...). A method would interfere with methods of the same name on the contents of the locked data.
impl<'a, R: RawMutexFair + 'a, T: ?Sized + 'a> MappedMutexGuard<'a, R, T>
fn unlock_fair(s: Self)Unlocks the mutex using a fair unlock protocol.
By default, mutexes are unfair and allow the current thread to re-lock the mutex before another has the chance to acquire the lock, even if that thread has been blocked on the mutex for a long time. This is the default because it allows much higher throughput as it avoids forcing a context switch on every mutex unlock. This can result in one thread acquiring a mutex many more times than other threads.
However in some cases it can be beneficial to ensure fairness by forcing the lock to pass on to a waiting thread if there is one. This is done by using this method instead of dropping the
MutexGuardnormally.
Trait Implementations
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Deref for MappedMutexGuard<'a, R, T>
type Target = T;fn deref(&self) -> &T
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> DerefMut for MappedMutexGuard<'a, R, T>
fn deref_mut(&mut self) -> &mut T
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Drop for MappedMutexGuard<'a, R, T>
fn drop(&mut self)
impl<'a, R: RawMutex + 'a, T: ?Sized + Send + 'a> Send for MappedMutexGuard<'a, R, T>
where
R::GuardMarker: Send,
impl<'a, R: RawMutex + 'a, T: Debug + ?Sized + 'a> Debug for MappedMutexGuard<'a, R, T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<'a, R: RawMutex + 'a, T: Display + ?Sized + 'a> Display for MappedMutexGuard<'a, R, T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<'a, R: RawMutex + Sync + 'a, T: ?Sized + Sync + 'a> Sync for MappedMutexGuard<'a, R, T>
Auto Trait Implementations
impl<'a, R, T> !UnwindSafe for MappedMutexGuard<'a, R, T>
impl<'a, R, T> Freeze for MappedMutexGuard<'a, R, T>
where
&'a R: Freeze,
*mut T: Freeze,
PhantomData<&'a mut T>: Freeze,
T: ?Sized,
impl<'a, R, T> RefUnwindSafe for MappedMutexGuard<'a, R, T>
where
&'a R: RefUnwindSafe,
*mut T: RefUnwindSafe,
PhantomData<&'a mut T>: RefUnwindSafe,
T: ?Sized,
impl<'a, R, T> Unpin for MappedMutexGuard<'a, R, T>
where
&'a R: Unpin,
*mut T: Unpin,
PhantomData<&'a mut T>: Unpin,
T: ?Sized,
impl<'a, R, T> UnsafeUnpin for MappedMutexGuard<'a, R, T>
where
&'a R: UnsafeUnpin,
*mut T: UnsafeUnpin,
PhantomData<&'a mut T>: UnsafeUnpin,
T: ?Sized,
Blanket Implementations
impl<P, T> Receiver for MappedMutexGuard<'a, R, T>
where
P: Deref<Target = T> + ?Sized,
T: ?Sized,
type Target = T;
impl<T> Any for MappedMutexGuard<'a, R, T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for MappedMutexGuard<'a, R, T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for MappedMutexGuard<'a, R, T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for MappedMutexGuard<'a, R, T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for MappedMutexGuard<'a, R, T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for MappedMutexGuard<'a, R, T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for MappedMutexGuard<'a, R, T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>