Expand description
An RAII implementation of a “scoped lock” of a mutex. When this structure is dropped (falls out of scope), the lock will be unlocked.
The data protected by the mutex can be accessed through this guard via its
Deref
and DerefMut
implementations.
Implementations
impl<'a, R, T> MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
impl<'a, R, T> MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
pub fn mutex(s: &MutexGuard<'a, R, T>) -> &'a Mutex<R, T>
pub fn mutex(s: &MutexGuard<'a, R, T>) -> &'a Mutex<R, T>
Returns a reference to the original Mutex
object.
pub fn map<U, F>(s: MutexGuard<'a, R, T>, f: F) -> MappedMutexGuard<'a, R, U> where
F: for<'_> FnOnce(&mut T) -> &mut U,
U: ?Sized,
pub fn map<U, F>(s: MutexGuard<'a, R, T>, f: F) -> MappedMutexGuard<'a, R, U> where
F: for<'_> FnOnce(&mut T) -> &mut U,
U: ?Sized,
Makes a new MappedMutexGuard
for a component of the locked data.
This operation cannot fail as the MutexGuard
passed
in already locked the mutex.
This is an associated function that needs to be
used as MutexGuard::map(...)
. A method would interfere with methods of
the same name on the contents of the locked data.
pub fn try_map<U, F>(
s: MutexGuard<'a, R, T>,
f: F
) -> Result<MappedMutexGuard<'a, R, U>, MutexGuard<'a, R, T>> where
F: for<'_> FnOnce(&mut T) -> Option<&mut U>,
U: ?Sized,
pub fn try_map<U, F>(
s: MutexGuard<'a, R, T>,
f: F
) -> Result<MappedMutexGuard<'a, R, U>, MutexGuard<'a, R, T>> where
F: for<'_> FnOnce(&mut T) -> Option<&mut U>,
U: ?Sized,
Attempts to make a new MappedMutexGuard
for a component of the
locked data. The original guard is returned if the closure returns None
.
This operation cannot fail as the MutexGuard
passed
in already locked the mutex.
This is an associated function that needs to be
used as MutexGuard::try_map(...)
. A method would interfere with methods of
the same name on the contents of the locked data.
pub fn unlocked<F, U>(s: &mut MutexGuard<'a, R, T>, f: F) -> U where
F: FnOnce() -> U,
pub fn unlocked<F, U>(s: &mut MutexGuard<'a, R, T>, f: F) -> U where
F: FnOnce() -> U,
Temporarily unlocks the mutex to execute the given function.
This is safe because &mut
guarantees that there exist no other
references to the data protected by the mutex.
pub fn leak(s: MutexGuard<'a, R, T>) -> &'a mut T
pub fn leak(s: MutexGuard<'a, R, T>) -> &'a mut T
Leaks the mutex guard and returns a mutable reference to the data protected by the mutex.
This will leave the Mutex
in a locked state.
impl<'a, R, T> MutexGuard<'a, R, T> where
R: 'a + RawMutexFair,
T: 'a + ?Sized,
impl<'a, R, T> MutexGuard<'a, R, T> where
R: 'a + RawMutexFair,
T: 'a + ?Sized,
pub fn unlock_fair(s: MutexGuard<'a, R, T>)
pub fn unlock_fair(s: MutexGuard<'a, R, T>)
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 MutexGuard
normally.
pub fn unlocked_fair<F, U>(s: &mut MutexGuard<'a, R, T>, f: F) -> U where
F: FnOnce() -> U,
pub fn unlocked_fair<F, U>(s: &mut MutexGuard<'a, R, T>, f: F) -> U where
F: FnOnce() -> U,
Temporarily unlocks the mutex to execute the given function.
The mutex is unlocked using a fair unlock protocol.
This is safe because &mut
guarantees that there exist no other
references to the data protected by the mutex.
pub fn bump(s: &mut MutexGuard<'a, R, T>)
pub fn bump(s: &mut MutexGuard<'a, R, T>)
Temporarily yields the mutex to a waiting thread if there is one.
This method is functionally equivalent to calling unlock_fair
followed
by lock
, however it can be much more efficient in the case where there
are no waiting threads.
Trait Implementations
impl<'a, R, T> Debug for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Debug + ?Sized,
impl<'a, R, T> Debug for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Debug + ?Sized,
impl<'a, R, T> Deref for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
impl<'a, R, T> Deref for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
impl<'a, R, T> DerefMut for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
impl<'a, R, T> DerefMut for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
impl<'a, R, T> Display for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Display + ?Sized,
impl<'a, R, T> Display for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + Display + ?Sized,
impl<'a, R, T> Drop for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
impl<'a, R, T> Drop for MutexGuard<'a, R, T> where
R: 'a + RawMutex,
T: 'a + ?Sized,
impl<'l, T> RelockMutexGuard for MutexGuard<'l, RawFairMutex, T> where
T: Send,
impl<'l, T> RelockMutexGuard for MutexGuard<'l, RawFairMutex, T> where
T: Send,
type MutexRef = &'l Mutex<RawFairMutex, T>
type MutexRef = &'l Mutex<RawFairMutex, T>
The reference to the mutex, recovered after unlocking.
type JustGuard = MutexGuard<'l, RawFairMutex, T>
type JustGuard = MutexGuard<'l, RawFairMutex, T>
The actual guard type. Read more
type LockFuture = Pin<Box<dyn Future<Output = <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::JustGuard> + Send + 'l, Global>>
type LockFuture = Pin<Box<dyn Future<Output = <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::JustGuard> + Send + 'l, Global>>
The type of the relock future.
fn unlock_for_relock(
self
) -> <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::MutexRef
fn unlock_for_relock(
self
) -> <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::MutexRef
Unlock the mutex and return a reference tt.
fn lock(
l: <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::MutexRef
) -> <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::LockFuture
fn lock(
l: <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::MutexRef
) -> <MutexGuard<'l, RawFairMutex, T> as RelockMutexGuard>::LockFuture
Relock the mutex, given a reference. Read more
impl<'l, T> RelockMutexGuard for MutexGuard<'l, RawMutex, T> where
T: Send,
impl<'l, T> RelockMutexGuard for MutexGuard<'l, RawMutex, T> where
T: Send,
type JustGuard = MutexGuard<'l, RawMutex, T>
type JustGuard = MutexGuard<'l, RawMutex, T>
The actual guard type. Read more
type LockFuture = Pin<Box<dyn Future<Output = <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::JustGuard> + Send + 'l, Global>>
type LockFuture = Pin<Box<dyn Future<Output = <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::JustGuard> + Send + 'l, Global>>
The type of the relock future.
fn unlock_for_relock(
self
) -> <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::MutexRef
fn unlock_for_relock(
self
) -> <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::MutexRef
Unlock the mutex and return a reference tt.
fn lock(
l: <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::MutexRef
) -> <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::LockFuture
fn lock(
l: <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::MutexRef
) -> <MutexGuard<'l, RawMutex, T> as RelockMutexGuard>::LockFuture
Relock the mutex, given a reference. Read more
impl<'a, R, T> Sync for MutexGuard<'a, R, T> where
R: 'a + RawMutex + Sync,
T: 'a + Sync + ?Sized,
Auto Trait Implementations
impl<'a, R, T> !RefUnwindSafe for MutexGuard<'a, R, T>
impl<'a, R, T: ?Sized> Send for MutexGuard<'a, R, T> where
R: Sync,
T: Send,
<R as RawMutex>::GuardMarker: Send,
impl<'a, R, T: ?Sized> Unpin for MutexGuard<'a, R, T> where
<R as RawMutex>::GuardMarker: Unpin,
impl<'a, R, T> !UnwindSafe for MutexGuard<'a, R, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
impl<A> DynCastExt for A
impl<A> DynCastExt for A
fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
Use this to cast from one trait object type to another. Read more
fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
Use this to upcast a trait to one of its supertraits. Read more
fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more