Struct Mutex

pub struct Mutex<T: ?Sized> { /* private fields */ }

A futures-aware mutex.

Fairness

This mutex provides no fairness guarantees. Tasks may not acquire the mutex in the order that they requested the lock, and it's possible for a single task which repeatedly takes the lock to starve other tasks, which may be left waiting indefinitely.

Implementations

impl<T> Mutex<T>

const fn new(t: T) -> Self

Creates a new futures-aware mutex.

fn into_inner(self) -> T

Consumes this mutex, returning the underlying data.

Examples

use futures::lock::Mutex;

let mutex = Mutex::new(0);
assert_eq!(mutex.into_inner(), 0);

impl<T: ?Sized> Mutex<T>

fn try_lock(&self) -> Option<MutexGuard<'_, T>>

Attempt to acquire the lock immediately.

If the lock is currently held, this will return None.

fn try_lock_owned(self: &Arc<Self>) -> Option<OwnedMutexGuard<T>>

Attempt to acquire the lock immediately.

If the lock is currently held, this will return None.

fn lock(&self) -> MutexLockFuture<'_, T>

Acquire the lock asynchronously.

This method returns a future that will resolve once the lock has been successfully acquired.

fn lock_owned(self: Arc<Self>) -> OwnedMutexLockFuture<T>

Acquire the lock asynchronously.

This method returns a future that will resolve once the lock has been successfully acquired.

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

Returns a mutable reference to the underlying data.

Since this call borrows the Mutex mutably, no actual locking needs to take place -- the mutable borrow statically guarantees no locks exist.

Examples

# futures::executor::block_on(async {
use futures::lock::Mutex;

let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock().await, 10);
# });

Trait Implementations

impl<T> From<T> for Mutex<T>

fn from(t: T) -> Self

impl<T: ?Sized + Send> Send for Mutex<T>

impl<T: ?Sized + Send> Sync for Mutex<T>

impl<T: ?Sized> Debug for Mutex<T>

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

impl<T: Default> Default for Mutex<T>

fn default() -> Self

Auto Trait Implementations

impl<T> !Freeze for Mutex<T>

impl<T> !RefUnwindSafe for Mutex<T>

impl<T> Unpin for Mutex<T> where UnsafeCell<T>: Unpin, T: ?Sized,

impl<T> UnsafeUnpin for Mutex<T> where UnsafeCell<T>: UnsafeUnpin, T: ?Sized,

impl<T> UnwindSafe for Mutex<T> where UnsafeCell<T>: UnwindSafe, T: ?Sized,

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

impl<T> From<T> for Mutex<T>

fn from(t: T) -> T

Returns the argument unchanged.

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

fn into(self) -> U

Calls U::from(self).

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

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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