pub struct MaybeEngineCell<T> { /* private fields */ }Expand description
A Sync wrapper that may or may not contain a value.
§When to choose this vs EngineCell
Use this when the global state has a clear “not yet installed” or
“already torn down” state. Access is gated on the inner Option
being Some, so all the try_* accessors return Option<...>,
matching core::reactive::hook::try_get_current’s wording.
§Field access rule
Same as EngineCell: inner is reached only through the
hand-written get_inner / set_inner accessors; the
struct-literal escape hatch is reserved for the new / default
constructors.
Implementations§
Source§impl<T> MaybeEngineCell<T>
Accessor implementations for MaybeEngineCell.
impl<T> MaybeEngineCell<T>
Accessor implementations for MaybeEngineCell.
Sourcepub fn get_inner(&self) -> &UnsafeCell<Option<T>>
pub fn get_inner(&self) -> &UnsafeCell<Option<T>>
Returns a shared reference to the backing UnsafeCell<Option<T>>.
Sourcepub fn set_inner(&mut self, val: UnsafeCell<Option<T>>) -> UnsafeCell<Option<T>>
pub fn set_inner(&mut self, val: UnsafeCell<Option<T>>) -> UnsafeCell<Option<T>>
Replaces the backing storage with val, returning the previous
UnsafeCell<Option<T>>.
§Arguments
val: UnsafeCell<Option<T>>- The new backing storage to install.
§Returns
UnsafeCell<Option<T>>- The previous backing storage.
Implemented via core::mem::replace. MaybeEngineCell<T>
carries the implicit Sized bound (it is T: Sized here
because Option<T> is Sized only when T is); mem::replace
therefore applies. The returned previous backing storage is
the caller’s responsibility to drop.
Note: set_inner is currently unused by the rest of the
module (the try_* paths go straight through raw pointer
reads/writes). It is kept here as the Lombok-shaped
counterpart for parity with EngineCell::get_inner, in case
future code wants to swap the whole backing storage.
Source§impl<T> MaybeEngineCell<T>
Constructor + accessors for MaybeEngineCell.
impl<T> MaybeEngineCell<T>
Constructor + accessors for MaybeEngineCell.
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates an empty cell.
Struct literal is permitted by the field-access rule for the constructor only.
Sourcepub fn try_get(&self) -> Option<&'static T>
pub fn try_get(&self) -> Option<&'static T>
If the cell contains a value, returns a shared reference to it.
Sourcepub fn try_get_mut(&self) -> Option<&'static mut T>
pub fn try_get_mut(&self) -> Option<&'static mut T>
If the cell contains a value, returns a mutable reference to it.
§Safety
Exclusivity rules from EngineCell::get_mut apply - no other
borrow on the same cell may be alive.
Sourcepub fn try_set(&self, value: T) -> Result<(), T>
pub fn try_set(&self, value: T) -> Result<(), T>
Installs value into the cell. Returns Err(value) if the cell
is already populated; the caller may then retry or drop it.
Reads through get_inner to inspect the current state without
aliasing, then writes through UnsafeCell::get raw pointer.
Sourcepub fn try_take(&self) -> Option<T>
pub fn try_take(&self) -> Option<T>
Removes and returns the contained value, leaving the cell empty.
Sourcepub fn try_replace(&self, value: T) -> Option<T>
pub fn try_replace(&self, value: T) -> Option<T>
Replaces the contained value, returning the old one.
Trait Implementations§
Source§impl<T> Default for MaybeEngineCell<T>
Default impl for MaybeEngineCell.
impl<T> Default for MaybeEngineCell<T>
Default impl for MaybeEngineCell.
impl<T> Sync for MaybeEngineCell<T>
Marker that MaybeEngineCell<T> is safe to share across the wasm
main thread under single-threaded access.