Struct static_cell::StaticCell

source ·
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.

See the crate-level docs for usage.

Implementations§

source§

impl<T> StaticCell<T>

source

pub const fn new() -> Self

Create a new, empty StaticCell.

It can be initialized at runtime with StaticCell::init() or similar methods.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

Trait Implementations§

source§

impl<T> Send for StaticCell<T>

source§

impl<T> Sync for StaticCell<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for StaticCell<T>

§

impl<T> Unpin for StaticCell<T>
where T: Unpin,

§

impl<T> UnwindSafe for StaticCell<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.