pub struct PhasedCell<T: Send + Sync> { /* private fields */ }Expand description
A cell that manages data through distinct Setup, Read, and Cleanup phases.
PhasedCell enforces a specific data lifecycle: initialization in the Setup
phase, a read-only operational period in the Read phase, and deconstruction
in the Cleanup phase.
This cell is Sync, allowing it to be shared across threads. During the Read
phase, the read and read_relaxed methods can be safely called from multiple
threads simultaneously. Access during phase transitions and mutable access in
other phases are not thread-safe.
Implementations§
Source§impl<T: Send + Sync> PhasedCell<T>
impl<T: Send + Sync> PhasedCell<T>
Sourcepub const fn new(data: T) -> Self
pub const fn new(data: T) -> Self
Creates a new PhasedCell in the Setup phase, containing the provided data.
§Examples
use setup_read_cleanup::PhasedCell;
let cell = PhasedCell::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, PhasedCell};
let cell = PhasedCell::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, PhasedCell};
let cell = PhasedCell::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.
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.
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 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 or if the closure
returns an error.
Sourcepub fn get_mut_unlocked(&self) -> Result<&mut T, PhasedError>
pub fn get_mut_unlocked(&self) -> Result<&mut T, PhasedError>
Returns a mutable reference to the contained data without acquiring a lock.
This method is only successful if the cell is in the Setup or Cleanup phase.
§Safety
This method does not use a lock to protect the data and is therefore not thread-safe.
It must only be called when you can guarantee that no other thread is accessing the
PhasedCell. Concurrent access can lead to data races and undefined behavior.
If you need to share the cell across threads and mutate it, use PhasedCellSync instead.