pub struct InitOnce<T> { /* private fields */ }
Expand description
A helper type that ensures a particular value is only initialized once.
Implementations§
source§impl<T> InitOnce<T>
impl<T> InitOnce<T>
sourcepub fn get(&self, initializer: impl FnOnce() -> T) -> &T
pub fn get(&self, initializer: impl FnOnce() -> T) -> &T
Gets the contents of this state, or initializes it if it has not already been initialized.
The initializer function is guaranteed to only be called once.
This function disables IRQs while it is initializing the inner value.
While this can cause audio skipping and other similar issues, it is
not normally a problem as interrupts will only be disabled once per
InitOnce
during the life cycle of the program.
sourcepub fn try_get<E>(
&self,
initializer: impl FnOnce() -> Result<T, E>
) -> Result<&T, E>
pub fn try_get<E>( &self, initializer: impl FnOnce() -> Result<T, E> ) -> Result<&T, E>
Gets the contents of this state, or initializes it if it has not already been initialized.
The initializer function is guaranteed to only be called once if it
returns Ok
. If it returns Err
, it will be called again in the
future until an attempt at initialization succeeds.
This function disables IRQs while it is initializing the inner value.
While this can cause audio skipping and other similar issues, it is
not normally a problem as interrupts will only be disabled once per
InitOnce
during the life cycle of the program.