Skip to main content

OwnedMappedMutexGuard

Struct OwnedMappedMutexGuard 

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

An owned mutex guard projected to a mutable component of the protected value.

OwnedMutexGuard::map and OwnedMutexGuard::filter_map create this guard. It keeps the original mutex alive and locked while exposing only the projected component.

§Examples

use std::sync::Arc;

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

struct Data {
    value: u32,
}

let data = Data { value: 42 };
let mutex = Arc::new(Mutex::new(data));
let guard = mutex.clone().lock_owned().await;
let value_guard = OwnedMutexGuard::map(guard, |data| &mut data.value);

assert_eq!(*value_guard, 42);

Implementations§

Source§

impl<T: ?Sized, U: ?Sized> OwnedMappedMutexGuard<T, U>

Source

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

Projects an owned mapped guard to a deeper mutable component.

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

§Examples
use std::sync::Arc;

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

#[derive(Debug)]
struct Config {
    host: String,
    port: u16,
}

let config = Config {
    host: "localhost".to_owned(),
    port: 8080,
};

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

// First map to config
let config_guard = OwnedMutexGuard::map(guard, |config| &mut config.host);
// Then map to the host string specifically
let host_guard = OwnedMappedMutexGuard::map(config_guard, |host| host.as_mut_str());

assert_eq!(&*host_guard, "localhost");
Source

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

Attempts to project an owned mapped guard to a deeper mutable component.

The original mapped guard is returned when f returns None. Call this as OwnedMappedMutexGuard::filter_map(...) so a method with the same name on U remains accessible through deref.

§Examples
use std::sync::Arc;

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

#[derive(Debug)]
struct Node {
    value: i32,
    left: Option<Box<Node>>,
    right: Option<Box<Node>>,
}

let node = Node {
    value: 10,
    left: Some(Box::new(Node {
        value: 5,
        left: None,
        right: None,
    })),
    right: None,
};

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

// First map to left child
let left_guard = OwnedMutexGuard::map(guard, |node| &mut node.left);
// Try to access the left child if it exists
let child_guard = OwnedMappedMutexGuard::filter_map(left_guard, |left| {
    left.as_mut().map(|boxed| boxed.as_mut())
})
.expect("left child should exist");

assert_eq!(child_guard.value, 5);

Trait Implementations§

Source§

impl<T: ?Sized, U: ?Sized + Debug> Debug for OwnedMappedMutexGuard<T, U>

Source§

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

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

impl<T: ?Sized, U: ?Sized> Deref for OwnedMappedMutexGuard<T, U>

Source§

type Target = U

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: ?Sized, U: ?Sized> DerefMut for OwnedMappedMutexGuard<T, U>

Source§

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

Mutably dereferences the value.
Source§

impl<T: ?Sized, U: ?Sized + Display> Display for OwnedMappedMutexGuard<T, U>

Source§

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

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

impl<T: ?Sized, U: ?Sized> Drop for OwnedMappedMutexGuard<T, U>

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, U: ?Sized + Send> Send for OwnedMappedMutexGuard<T, U>

Source§

impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Sync for OwnedMappedMutexGuard<T, U>

Auto Trait Implementations§

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.