pub struct PhasedCellSync<T: Send + Sync> { /* private fields */ }Expand description
A thread-safe cell that manages data through Setup, Read, and Cleanup phases
with support for concurrent mutable access.
PhasedCellSync is similar to PhasedCell but uses a std::sync::Mutex to
synchronize access to the internal data. This is particularly useful when
multiple threads need to mutate the data during the Setup or Cleanup
phases, which is not safely supported by PhasedCell.
Implementations§
Source§impl<T: Send + Sync> PhasedCellSync<T>
impl<T: Send + Sync> PhasedCellSync<T>
Sourcepub const fn new(data: T) -> Self
pub const fn new(data: T) -> Self
Creates a new PhasedCellSync in the Setup phase, containing the provided data.
§Examples
use setup_read_cleanup::PhasedCellSync;
let cell = PhasedCellSync::new(10);Sourcepub fn phase_relaxed(&self) -> Phase
pub fn phase_relaxed(&self) -> Phase
Returns the current phase of the cell with relaxed memory ordering.
This method is faster than phase but provides weaker memory ordering guarantees.
§Examples
use setup_read_cleanup::{Phase, PhasedCellSync};
let cell = PhasedCellSync::new(10);
assert_eq!(cell.phase_relaxed(), Phase::Setup);Sourcepub fn phase(&self) -> Phase
pub fn phase(&self) -> Phase
Returns the current phase of the cell with acquire memory ordering.
This method provides stronger memory ordering guarantees than phase_relaxed.
§Examples
use setup_read_cleanup::{Phase, PhasedCellSync};
let cell = PhasedCellSync::new(10);
assert_eq!(cell.phase(), Phase::Setup);Sourcepub fn read_relaxed(&self) -> Result<&T, PhasedError>
pub fn read_relaxed(&self) -> Result<&T, PhasedError>
Returns a reference to the contained data with relaxed memory ordering.
This method is only successful if the cell is in the Read phase.
It provides weaker memory ordering guarantees.
§Errors
Returns an error if the cell is not in the Read phase or the data is unavailable.
Sourcepub fn read(&self) -> Result<&T, PhasedError>
pub fn read(&self) -> Result<&T, PhasedError>
Returns a reference to the contained data with acquire memory ordering.
This method is only successful if the cell is in the Read phase.
It provides stronger memory ordering guarantees.
§Errors
Returns an error if the cell is not in the Read phase or the data is unavailable.
Sourcepub fn transition_to_cleanup<F, E>(&self, f: F) -> Result<(), PhasedError>
pub fn transition_to_cleanup<F, E>(&self, f: F) -> Result<(), PhasedError>
Transitions the cell to the Cleanup phase.
This method takes a closure f which is executed on the contained data.
This can be called from the Setup or Read phase.
§Errors
Returns an error if the phase transition fails, the mutex is poisoned, or the closure returns an error.
Sourcepub fn transition_to_read<F, E>(&self, f: F) -> Result<(), PhasedError>
pub fn transition_to_read<F, E>(&self, f: F) -> Result<(), PhasedError>
Transitions the cell from the Setup phase to the Read phase.
This method takes a closure f which is executed on the contained data
during the transition.
§Errors
Returns an error if the cell is not in the Setup phase, the mutex is
poisoned, or if the closure returns an error.
Sourcepub fn lock(&self) -> Result<StdMutexGuard<'_, T>, PhasedError>
pub fn lock(&self) -> Result<StdMutexGuard<'_, T>, PhasedError>
Locks the cell and returns a guard that allows mutable access to the data.
This method is only successful if the cell is in the Setup or Cleanup phase.
The returned guard releases the lock when it is dropped.
§Errors
Returns an error if the cell is in the Read phase, is transitioning,
or if the mutex is poisoned.