pub struct StaticCell<T> { /* private fields */ }Expand description
Statically allocated, initialized at runtime cell.
It has two states: “empty” and “full”. It is created “empty”, and obtaining a reference to the contents permanently changes it to “full”. This allows that reference to be valid forever.
If your value can be initialized as a const value, consider using ConstStaticCell
instead if you only need to take the value at runtime.
See the crate-level docs for usage.
Implementations§
Source§impl<T> StaticCell<T>
impl<T> StaticCell<T>
Sourcepub const fn new() -> StaticCell<T>
pub const fn new() -> StaticCell<T>
Create a new, empty StaticCell.
It can be initialized at runtime with StaticCell::init() or similar methods.
Sourcepub fn init(&'static self, val: T) -> &'static mut T
pub fn init(&'static self, val: T) -> &'static mut T
Initialize the StaticCell with a value, returning a mutable reference to it.
Using this method, the compiler usually constructs val in the stack and then moves
it into the StaticCell. If T is big, this is likely to cause stack overflows.
Considering using StaticCell::init_with instead, which will construct it in-place inside the StaticCell.
§Panics
Panics if this StaticCell is already full.
Sourcepub fn init_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T
pub fn init_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T
Initialize the StaticCell with the closure’s return value, returning a mutable reference to it.
The advantage over StaticCell::init is that this method allows the closure to construct
the T value in-place directly inside the StaticCell, saving stack space.
§Panics
Panics if this StaticCell is already full.
Sourcepub fn uninit(&'static self) -> &'static mut MaybeUninit<T>
pub fn uninit(&'static self) -> &'static mut MaybeUninit<T>
Return a mutable reference to the uninitialized memory owned by the StaticCell.
Using this method directly is not recommended, but it can be used to construct T in-place directly
in a guaranteed fashion.
§Panics
Panics if this StaticCell is already full.
Sourcepub fn try_init(&'static self, val: T) -> Option<&'static mut T>
pub fn try_init(&'static self, val: T) -> Option<&'static mut T>
Try initializing the StaticCell with a value, returning a mutable reference to it.
If this StaticCell is already full, it returns None.
Using this method, the compiler usually constructs val in the stack and then moves
it into the StaticCell. If T is big, this is likely to cause stack overflows.
Considering using StaticCell::try_init_with instead, which will construct it in-place inside the StaticCell.
Will only return a Some(&’static mut T) when the StaticCell was not yet initialized.
Sourcepub fn try_init_with(
&'static self,
val: impl FnOnce() -> T,
) -> Option<&'static mut T>
pub fn try_init_with( &'static self, val: impl FnOnce() -> T, ) -> Option<&'static mut T>
Try initializing the StaticCell with the closure’s return value, returning a mutable reference to it.
If this StaticCell is already full, it returns None.
The advantage over StaticCell::init is that this method allows the closure to construct
the T value in-place directly inside the StaticCell, saving stack space.
Sourcepub fn try_uninit(&'static self) -> Option<&'static mut MaybeUninit<T>>
pub fn try_uninit(&'static self) -> Option<&'static mut MaybeUninit<T>>
Try returning a mutable reference to the uninitialized memory owned by the StaticCell.
If this StaticCell is already full, it returns None.
Using this method directly is not recommended, but it can be used to construct T in-place directly
in a guaranteed fashion.