pub struct HeldSyncCell<T> { /* private fields */ }
Expand description
A cell that holds a value until any changes made are applied by use of the update
method.
Getting the value or obtaining a reference to the value in this cell will return the value
immediately following the last call to update
. This allows for mutably altering a value while
keeping a reference for a short amount of time to the old value.
This is useful when you want a method in a structure to be able to modify the structure it is
being called from such as when changing the scene in a game engine.
§Usage
use sync_cell::HeldSyncCell;
let cell = HeldSyncCell::new(0);
// Set the next value of the cell.
cell.set(1);
// Cell continues to hold a value of 0 until the `update` method is called.
assert_eq!(0, cell.get());
cell.update();
assert_eq!(1, cell.get());
Implementations§
Source§impl<T> HeldSyncCell<T>
impl<T> HeldSyncCell<T>
Sourcepub const fn new(data: T) -> Self
pub const fn new(data: T) -> Self
Creates a new HeldSyncCell
.
data
- The initial value of theHeldSyncCell
.
Sourcepub fn set(&self, value: T)
pub fn set(&self, value: T)
Sets the value contained in this cell.
This value will only become available once the update
method is called.
In the case that multiple threads call this method simultaniously, the order in which the calls are processed is not defined. However, the final result will be the value specified by one of the method calls.
value
- The new value of the cell.
§Panicking
This method will panic if any of the locks become poisoned.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Retrieves the inner value stored in this HeldSyncCell
.
This will return the most up-to-date value even if update
has not been called.
§Panicking
This method will panic if any of the locks become poisoned.
Sourcepub fn borrow(&self) -> RwLockReadGuard<'_, T>
pub fn borrow(&self) -> RwLockReadGuard<'_, T>
Borrows a immutable reference to the data stored in this cell. This is a reference to the current value of the cell.
§Panicking
This method will panic if any of the locks become poisoned.
Sourcepub fn borrow_mut(&self) -> RwLockWriteGuard<'_, T>
pub fn borrow_mut(&self) -> RwLockWriteGuard<'_, T>
Borrows a mutable reference to the data stored in this cell. This is a reference to the current value of the cell not the incoming value. Any changes to the value will update the current value.
§Panicking
This method will panic if any of the locks become poisoned.
Sourcepub fn has_update(&self) -> bool
pub fn has_update(&self) -> bool
Checks if a new nalue is available that can be applied by calling update
.
§Panicking
This method will panic if any of the locks become poisoned.