Struct lock_cell::LockCell

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

source

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

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

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

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

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

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>

source

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

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

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

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

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

pub fn reset_lock(&mut self) -> &mut T

Resets the lock state, in case that any LockGuards 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);

Trait Implementations§

source§

impl<T: Debug> Debug for LockCell<T>

source§

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

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

impl<T: Default> Default for LockCell<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> From<T> for LockCell<T>

source§

fn from(value: T) -> Self

Converts to this type from the input type.
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.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for LockCell<T>

§

impl<T: ?Sized> Send for LockCell<T>where T: Send,

§

impl<T> !Sync for LockCell<T>

§

impl<T: ?Sized> Unpin for LockCell<T>where T: Unpin,

§

impl<T: ?Sized> UnwindSafe for LockCell<T>where T: UnwindSafe,

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<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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, 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.