Struct StaticCell
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§
§impl<T> StaticCell<T>
impl<T> StaticCell<T>
pub const fn new() -> StaticCell<T>
Available on crate feature threading
only.
pub const fn new() -> StaticCell<T>
threading
only.Create a new, empty StaticCell
.
It can be initialized at runtime with StaticCell::init()
or similar methods.
pub fn init(&'static self, val: T) -> &'static mut T
Available on crate feature threading
only.
pub fn init(&'static self, val: T) -> &'static mut T
threading
only.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.
pub fn init_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T
Available on crate feature threading
only.
pub fn init_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T
threading
only.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.
pub fn uninit(&'static self) -> &'static mut MaybeUninit<T>
Available on crate feature threading
only.
pub fn uninit(&'static self) -> &'static mut MaybeUninit<T>
threading
only.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.
pub fn try_init(&'static self, val: T) -> Option<&'static mut T>
Available on crate feature threading
only.
pub fn try_init(&'static self, val: T) -> Option<&'static mut T>
threading
only.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.
pub fn try_init_with(
&'static self,
val: impl FnOnce() -> T,
) -> Option<&'static mut T>
Available on crate feature threading
only.
pub fn try_init_with( &'static self, val: impl FnOnce() -> T, ) -> Option<&'static mut T>
threading
only.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.
pub fn try_uninit(&'static self) -> Option<&'static mut MaybeUninit<T>>
Available on crate feature threading
only.
pub fn try_uninit(&'static self) -> Option<&'static mut MaybeUninit<T>>
threading
only.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.