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>
impl<T: ?Sized, U: ?Sized> OwnedMappedMutexGuard<T, U>
Sourcepub fn map<V, F>(orig: Self, f: F) -> OwnedMappedMutexGuard<T, V>
pub fn map<V, F>(orig: Self, f: F) -> OwnedMappedMutexGuard<T, V>
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");Sourcepub fn filter_map<V, F>(
orig: Self,
f: F,
) -> Result<OwnedMappedMutexGuard<T, V>, Self>
pub fn filter_map<V, F>( orig: Self, f: F, ) -> Result<OwnedMappedMutexGuard<T, V>, Self>
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§
impl<T: ?Sized + Send, U: ?Sized + Send> Send for OwnedMappedMutexGuard<T, U>
impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Sync for OwnedMappedMutexGuard<T, U>
Auto Trait Implementations§
impl<T, U> !RefUnwindSafe for OwnedMappedMutexGuard<T, U>
impl<T, U> !UnwindSafe for OwnedMappedMutexGuard<T, U>
impl<T, U> Freeze for OwnedMappedMutexGuard<T, U>
impl<T, U> Unpin for OwnedMappedMutexGuard<T, U>
impl<T, U> UnsafeUnpin for OwnedMappedMutexGuard<T, U>where
Arc<Mutex<T>>: UnsafeUnpin,
NonNull<U>: UnsafeUnpin,
PhantomData<*mut U>: UnsafeUnpin,
T: ?Sized,
U: ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more