tokio::sync

Struct MutexGuard

Source
pub struct MutexGuard<'a, T: ?Sized> { /* private fields */ }
Available on crate feature sync only.
Expand description

A handle to a held Mutex. The guard can be held across any .await point as it is Send.

As long as you have this guard, you have exclusive access to the underlying T. The guard internally borrows the Mutex, so the mutex will not be dropped while a guard exists.

The lock is automatically released whenever the guard is dropped, at which point lock will succeed yet again.

Implementations§

Source§

impl<'a, T: ?Sized> MutexGuard<'a, T>

Source

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

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.

§Examples
use tokio::sync::{Mutex, MutexGuard};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Foo(u32);

let foo = Mutex::new(Foo(1));

{
    let mut mapped = MutexGuard::map(foo.lock().await, |f| &mut f.0);
    *mapped = 2;
}

assert_eq!(Foo(2), *foo.lock().await);
Source

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

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.

§Examples
use tokio::sync::{Mutex, MutexGuard};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Foo(u32);

let foo = Mutex::new(Foo(1));

{
    let mut mapped = MutexGuard::try_map(foo.lock().await, |f| Some(&mut f.0))
        .expect("should not fail");
    *mapped = 2;
}

assert_eq!(Foo(2), *foo.lock().await);
Source

pub fn mutex(this: &Self) -> &'a Mutex<T>

Returns a reference to the original Mutex.

use tokio::sync::{Mutex, MutexGuard};

async fn unlock_and_relock<'l>(guard: MutexGuard<'l, u32>) -> MutexGuard<'l, u32> {
    println!("1. contains: {:?}", *guard);
    let mutex = MutexGuard::mutex(&guard);
    drop(guard);
    let guard = mutex.lock().await;
    println!("2. contains: {:?}", *guard);
    guard
}

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§

impl<T> Sync for MutexGuard<'_, T>
where T: ?Sized + Send + Sync,

Auto Trait Implementations§

§

impl<'a, T> Freeze for MutexGuard<'a, T>
where T: ?Sized,

§

impl<'a, T> !RefUnwindSafe for MutexGuard<'a, T>

§

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

§

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

§

impl<'a, T> !UnwindSafe for MutexGuard<'a, T>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> ToString for T
where T: Display + ?Sized,

Source§

default 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more