Struct lock_cell::LockGuard

source ·
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>

source

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);
source

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> AsMut<T> for LockGuard<'lock, T>

source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<'lock, T: ?Sized> AsRef<T> for LockGuard<'lock, T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<'lock, T: ?Sized> Borrow<T> for LockGuard<'lock, T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<'lock, T: ?Sized> BorrowMut<T> for LockGuard<'lock, T>

source§

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

Mutably borrows from an owned value. Read more
source§

impl<'lock, T: Debug + ?Sized> Debug for LockGuard<'lock, T>

source§

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

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

impl<'lock, T: ?Sized> Deref for LockGuard<'lock, T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<'lock, T: ?Sized> DerefMut for LockGuard<'lock, T>

source§

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

Mutably dereferences the value.
source§

impl<'lock, T: Display + ?Sized> Display for LockGuard<'lock, T>

source§

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

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

impl<'lock, T: ?Sized> Drop for LockGuard<'lock, T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'lock, T: ?Sized + Hash> Hash for LockGuard<'lock, T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'lock, T: ?Sized + Ord> Ord for LockGuard<'lock, T>

source§

fn cmp(&self, other: &LockGuard<'lock, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<'other, 'lock, T: ?Sized + PartialEq> PartialEq<LockGuard<'other, T>> for LockGuard<'lock, T>

source§

fn eq(&self, other: &LockGuard<'other, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'lock, T: ?Sized + PartialEq> PartialEq<T> for LockGuard<'lock, T>

source§

fn eq(&self, other: &T) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'other, 'lock, T: ?Sized + PartialOrd> PartialOrd<LockGuard<'other, T>> for LockGuard<'lock, T>

source§

fn partial_cmp(&self, other: &LockGuard<'other, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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 more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'lock, T: ?Sized + PartialOrd> PartialOrd<T> for LockGuard<'lock, T>

source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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 more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'lock, T> TryFrom<&'lock LockCell<T>> for LockGuard<'lock, T>

§

type Error = TryLockError

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

fn try_from(lock_cell: &'lock LockCell<T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

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

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

Performs the conversion.
source§

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

§

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.