Skip to main content

OwnedMutexGuard

Struct OwnedMutexGuard 

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

A proof of exclusive access that owns an Arc containing its Mutex.

Mutex::lock_owned and Mutex::try_lock_owned create this guard. Owning the Arc lets the guard outlive the reference used to acquire it; dropping the guard releases the lock and its share of the Arc.

Implementations§

Source§

impl<T: ?Sized> OwnedMutexGuard<T>

Source

pub fn map<U, F>(orig: Self, f: F) -> OwnedMappedMutexGuard<T, U>
where F: FnOnce(&mut T) -> &mut U, U: ?Sized,

Projects this guard to a mutable component of the protected value.

The returned guard retains the same Arc and keeps the mutex locked. Call this as OwnedMutexGuard::map(...) so a method named map on T remains accessible through deref.

§Examples
use std::sync::Arc;

use asyncband::mutex::Mutex;
use asyncband::mutex::OwnedMutexGuard;

struct Config {
    name: String,
    value: u32,
}

let config = Config {
    name: "front size".to_owned(),
    value: 42,
};

let mutex = Arc::new(Mutex::new(config));
let guard = mutex.clone().lock_owned().await;

// Map to access only the value field
let value_guard = OwnedMutexGuard::map(guard, |config| &mut config.value);
assert_eq!(*value_guard, 42);
Source

pub fn filter_map<U, F>( orig: Self, f: F, ) -> Result<OwnedMappedMutexGuard<T, U>, Self>
where F: FnOnce(&mut T) -> Option<&mut U>, U: ?Sized,

Attempts to project this guard to a mutable component of the protected value.

The original guard is returned when f returns None. Call this as OwnedMutexGuard::filter_map(...) so a method with the same name on T remains accessible.

§Examples
use std::sync::Arc;

use asyncband::mutex::Mutex;
use asyncband::mutex::OwnedMutexGuard;

let data = vec![1, 2, 3, 4, 5];
let mutex = Arc::new(Mutex::new(data));
let guard = mutex.clone().lock_owned().await;

// Map to the first element
let first_guard =
    OwnedMutexGuard::filter_map(guard, |vec| vec.get_mut(0)).expect("vec should not be empty");

assert_eq!(*first_guard, 1);

Trait Implementations§

Source§

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

Source§

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

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

impl<T: ?Sized> Deref for OwnedMutexGuard<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 OwnedMutexGuard<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: ?Sized + Display> Display for OwnedMutexGuard<T>

Source§

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

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

impl<T: ?Sized> Drop for OwnedMutexGuard<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for OwnedMutexGuard<T>

§

impl<T> !UnwindSafe for OwnedMutexGuard<T>

§

impl<T> Freeze for OwnedMutexGuard<T>
where Arc<Mutex<T>>: Freeze, T: ?Sized,

§

impl<T> Send for OwnedMutexGuard<T>
where Arc<Mutex<T>>: Send, T: ?Sized,

§

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

§

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

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, 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.