pub struct LockCell<T: ?Sized> { /* private fields */ }
Expand description
A mutable memory location with dynamically checked borrow rules.
See the module level documentation for more.
Implementations§
source§impl<T> LockCell<T>
impl<T> LockCell<T>
sourcepub const fn new(value: T) -> Self
pub const fn new(value: T) -> Self
Create a new LockCell
with the given value
.
Examples
use lock_cell::LockCell;
let cell = LockCell::new("I could be anything!".to_string());
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the LockCell
, returning the inner value.
Examples
use lock_cell::LockCell;
let cell = LockCell::new(5);
let five = cell.into_inner();
assert_eq!(five, 5);
sourcepub fn swap(&self, rhs: &LockCell<T>)
pub fn swap(&self, rhs: &LockCell<T>)
Swaps the wrapped values of self
and rhs
.
This function corresponds to std::mem::swap
.
Panics
Panics if either LockCell
is locked, or if self
and rhs
point to the same
LockCell
.
Examples
use lock_cell::LockCell;
let cell_1 = LockCell::new(3);
let cell_2 = LockCell::new(24);
cell_1.swap(&cell_2);
assert_eq!(cell_1.into_inner(), 24);
assert_eq!(cell_2.into_inner(), 3);
sourcepub fn replace(&self, new_value: T) -> T
pub fn replace(&self, new_value: T) -> T
Sets the value in this LockCell
to new_value
, returning the previous value
in the LockCell
.
Panics
This method will panic if the cell is locked.
Examples
use lock_cell::LockCell;
let cell = LockCell::new(5);
let old_value = cell.replace(6);
assert_eq!(old_value, 5);
assert_eq!(cell.into_inner(), 6);
sourcepub fn replace_with<F>(&self, f: F) -> Twhere
F: FnOnce(&mut T) -> T,
pub fn replace_with<F>(&self, f: F) -> Twhere F: FnOnce(&mut T) -> T,
Replaces the wrapped value with a new value computed from the function f
,
returning the old value without deinitializing either.
Panics
This method will panic if the LockCell
is locked.
Examples
use lock_cell::LockCell;
let cell = LockCell::new(5);
let old_value = cell.replace_with(|old| {
*old += 1;
*old + 1
});
assert_eq!(old_value, 6);
assert_eq!(cell.into_inner(), 7);
sourcepub fn take(&self) -> Twhere
T: Default,
pub fn take(&self) -> Twhere T: Default,
Replaces the value in this LockCell
with the Default::default()
value,
returning the previous value in the LockCell
.
Panics
This method will panic if the cell is locked.
Examples
use lock_cell::LockCell;
let cell = LockCell::new(5);
let old_value = cell.take();
assert_eq!(old_value, 5);
assert_eq!(cell.into_inner(), 0);
source§impl<T: ?Sized> LockCell<T>
impl<T: ?Sized> LockCell<T>
sourcepub fn try_lock(&self) -> Result<LockGuard<'_, T>, TryLockError>
pub fn try_lock(&self) -> Result<LockGuard<'_, T>, TryLockError>
Attempt to lock the LockCell
.
Notes
If this LockCell
is not locked, the function succeeds and will return a
guard which provides mutable access to the inner value.
Errors
If the LockCell
is already locked, this function will fail and will
return a TryLockError
.
Examples
let cell = LockCell::new(21);
let first_access = cell.try_lock();
assert!(first_access.is_ok());
let first_lock = first_access?;
assert_eq!(*first_lock, 21);
let second_access = cell.try_lock();
assert!(second_access.is_err());
sourcepub fn lock(&self) -> LockGuard<'_, T>
pub fn lock(&self) -> LockGuard<'_, T>
Lock the given LockCell
, returning a LockGuard
which can be used to access
the value.
The LockCell
will be locked until the returned LockGuard
goes out of scope.
The cell can only have a single lock at a time active.
Panics
This method will panic if the LockCell
is already locked.
To avoid this, you can use the try_lock()
method to return a Result
to
check if the lock succeeded, or you can use the is_locked()
method to check
ahead of time if the lock will succeed.
Examples
use lock_cell::LockCell;
let cell = LockCell::new("Hello".to_string());
let lock = cell.lock();
assert_eq!(&*lock, "Hello");
sourcepub fn is_locked(&self) -> bool
pub fn is_locked(&self) -> bool
Returns whether this LockCell
is currently locked.
Examples
use lock_cell::LockCell;
let cell = LockCell::new(5);
assert!(!cell.is_locked());
let lock = cell.lock();
assert!(cell.is_locked());
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Provides mutable access to the inner value.
As this requires exclusive access to the LockCell
, no locking is
required to provide exclusive access to the value.
Examples
use lock_cell::LockCell;
let mut cell = LockCell::new(54);
*cell.get_mut() = 20;
assert_eq!(cell.into_inner(), 20);
sourcepub fn as_ptr(&self) -> *mut T
pub fn as_ptr(&self) -> *mut T
Return a raw pointer to the underlying data in this LockCell
.
Notes
This function does not lock the LockCell
. Therefore, any mutations made through
the returned pointer must be synchronized in some other way, or undefined behaviour
may occur.
Examples
use lock_cell::LockCell;
let cell = LockCell::new(5);
let ptr = cell.as_ptr();
sourcepub fn reset_lock(&mut self) -> &mut T
pub fn reset_lock(&mut self) -> &mut T
Resets the lock state, in case that any LockGuard
s have been leaked.
This method takes self
by &mut
to ensure that there are no other borrows
of the LockCell
in flight.
Examples
use lock_cell::LockCell;
let mut cell = LockCell::new(12);
let mut lock = cell.lock();
*lock = 54;
mem::forget(lock);
assert!(cell.is_locked());
cell.reset_lock();
assert!(!cell.is_locked());
assert_eq!(cell.into_inner(), 54);