pub struct LockGuard<'lock, T: ?Sized> { /* private fields */ }
Expand description
A LockGuard
provides exclusive access to the inner value of a LockCell<T>
.
An instance of this type can be constructed from a LockCell
using the LockCell::try_lock()
or LockCell::lock()
methods.
See the module level documentation for more.
Implementations§
source§impl<'lock, T: ?Sized> LockGuard<'lock, T>
impl<'lock, T: ?Sized> LockGuard<'lock, T>
sourcepub fn map<F, U: ?Sized>(this: Self, func: F) -> LockGuard<'lock, U>where
F: FnOnce(&mut T) -> &mut U,
pub fn map<F, U: ?Sized>(this: Self, func: F) -> LockGuard<'lock, U>where F: FnOnce(&mut T) -> &mut U,
Applies the given func
to the contents LockGuard
to return a new LockGuard
which
points to a sub-part of the original data.
Examples
use lock_cell::{LockCell, LockGuard};
let cell = LockCell::<(i32, i32)>::default();
let lock = cell.lock();
let mut value = LockGuard::map(lock, |(_, ref mut val)| val);
*value = 21;
drop(value);
let tuple = cell.into_inner();
assert_eq!(tuple.1, 21);
sourcepub fn filter_map<F, U: ?Sized>(
this: Self,
func: F
) -> Result<LockGuard<'lock, U>, Self>where
F: FnOnce(&mut T) -> Option<&mut U>,
pub fn filter_map<F, U: ?Sized>( this: Self, func: F ) -> Result<LockGuard<'lock, U>, Self>where F: FnOnce(&mut T) -> Option<&mut U>,
Applies the given func
to the contents of LockGuard
to return an optional reference
to a part of the original data.
Errors
If func
returns None
, then the original guard will be returned in the Err
variant
of the return value.
Examples
use lock_cell::{LockCell, LockGuard};
let cell = LockCell::new(Some(0));
let lock = cell.lock();
let mut value = match LockGuard::filter_map(lock, |value| value.as_mut()) {
Ok(inner) => inner,
Err(old_lock) => panic!("Unexpectedly empty value: {:?}", old_lock),
};
*value = 5;
drop(value);
let old_value = cell.replace(None);
assert_eq!(old_value, Some(5));
let lock = cell.lock();
let value = match LockGuard::filter_map(lock, |value| value.as_mut()) {
Ok(inner) => panic!("Unexpected value is present: {:?}", inner),
Err(old_lock) => old_lock,
};
assert_eq!(*value, None);
Trait Implementations§
source§impl<'lock, T: ?Sized> BorrowMut<T> for LockGuard<'lock, T>
impl<'lock, T: ?Sized> BorrowMut<T> for LockGuard<'lock, T>
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<'lock, T: ?Sized + Ord> Ord for LockGuard<'lock, T>
impl<'lock, T: ?Sized + Ord> Ord for LockGuard<'lock, T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl<'other, 'lock, T: ?Sized + PartialEq> PartialEq<LockGuard<'other, T>> for LockGuard<'lock, T>
impl<'other, 'lock, T: ?Sized + PartialEq> PartialEq<LockGuard<'other, T>> for LockGuard<'lock, T>
source§impl<'other, 'lock, T: ?Sized + PartialOrd> PartialOrd<LockGuard<'other, T>> for LockGuard<'lock, T>
impl<'other, 'lock, T: ?Sized + PartialOrd> PartialOrd<LockGuard<'other, T>> for LockGuard<'lock, T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl<'lock, T: ?Sized + PartialOrd> PartialOrd<T> for LockGuard<'lock, T>
impl<'lock, T: ?Sized + PartialOrd> PartialOrd<T> for LockGuard<'lock, T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl<'lock, T: ?Sized + Eq> Eq for LockGuard<'lock, T>
Auto Trait Implementations§
impl<'lock, T> !RefUnwindSafe for LockGuard<'lock, T>
impl<'lock, T> !Send for LockGuard<'lock, T>
impl<'lock, T> !Sync for LockGuard<'lock, T>
impl<'lock, T: ?Sized> Unpin for LockGuard<'lock, T>
impl<'lock, T> !UnwindSafe for LockGuard<'lock, T>
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