Mutex

Struct Mutex 

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

An async mutex for protecting shared data.

See the module level documentation for more.

Implementations§

Source§

impl<T> Mutex<T>

Source

pub const fn new(t: T) -> Self

Creates a new mutex in an unlocked state ready for use.

§Examples
use mea::mutex::Mutex;

let mutex = Mutex::new(5);
Source

pub fn into_inner(self) -> T

Consumes the mutex, returning the underlying data.

§Examples
use mea::mutex::Mutex;

let mutex = Mutex::new(1);
let n = mutex.into_inner();
assert_eq!(n, 1);
Source§

impl<T: ?Sized> Mutex<T>

Source

pub async fn lock(&self) -> MutexGuard<'_, T>

Locks this mutex, causing the current task to yield until the lock has been acquired. When the lock has been acquired, function returns a MutexGuard.

This method is async and will yield the current task if the mutex is currently held by another task. When the mutex becomes available, the task will be woken up and given the lock.

§Cancel safety

This method uses a queue to fairly distribute locks in the order they were requested. Cancelling a call to lock makes you lose your place in the queue.

§Examples
use mea::mutex::Mutex;

let mutex = Mutex::new(1);

let mut n = mutex.lock().await;
*n = 2;
Source

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

Attempts to acquire the lock, and returns None if the lock is currently held somewhere else.

§Examples
use mea::mutex::Mutex;

let mutex = Mutex::new(1);
let mut guard = mutex.try_lock().expect("mutex is locked");
*guard += 1;
assert_eq!(2, *guard);
Source

pub async fn lock_owned(self: Arc<Self>) -> OwnedMutexGuard<T>

Locks this mutex, causing the current task to yield until the lock has been acquired. When the lock has been acquired, this returns an OwnedMutexGuard.

This method is async and will yield the current task if the mutex is currently held by another task. When the mutex becomes available, the task will be woken up and given the lock.

This method is identical to Mutex::lock, except that the returned guard references the Mutex with an Arc rather than by borrowing it. Therefore, the Mutex must be wrapped in an Arc to call this method, and the guard will live for the 'static lifetime, as it keeps the Mutex alive by holding an Arc.

§Cancel safety

This method uses a queue to fairly distribute locks in the order they were requested. Cancelling a call to lock_owned makes you lose your place in the queue.

§Examples
use std::sync::Arc;

use mea::mutex::Mutex;

let mutex = Arc::new(Mutex::new(1));

let mut n = mutex.clone().lock_owned().await;
*n = 2;
Source

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

Attempts to acquire the lock, and returns None if the lock is currently held somewhere else.

This method is identical to Mutex::try_lock, except that the returned guard references the Mutex with an Arc rather than by borrowing it. Therefore, the Mutex must be wrapped in an Arc to call this method, and the guard will live for the 'static lifetime, as it keeps the Mutex alive by holding an Arc.

§Examples
use std::sync::Arc;

use mea::mutex::Mutex;

let mutex = Arc::new(Mutex::new(1));
let mut guard = mutex.clone().try_lock_owned().expect("mutex is locked");
*guard += 1;
assert_eq!(2, *guard);
Source

pub 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
use mea::mutex::Mutex;

let mut mutex = Mutex::new(1);
let n = mutex.get_mut();
*n = 2;

Trait Implementations§

Source§

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

Source§

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

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

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

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

fn from(t: T) -> Self

Converts to this type from the input type.
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

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

§

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

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<!> for T

§

fn from(t: !) -> T

Converts to this type from the input type.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for 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 T
where U: Into<T>,

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

type Error = <U as TryFrom<T>>::Error

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

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

Performs the conversion.