pub struct InitGuard<T> { /* private fields */ }
Expand description
A guard for when you want to store some data in a static, and explicitly initialize it at some point. This
is different from spin::Once
in that you do not need to provide an initialization method every time you
access the object (it also uses MaybeUninit
instead of Option
). This will only release shared
references to the data inside, so if you want to mutate it from mutable threads, you’ll need to use a type
like Mutex
or RwLock
within this.
Implementations§
Source§impl<T> InitGuard<T>
impl<T> InitGuard<T>
pub const fn uninit() -> InitGuard<T>
Sourcepub fn initialize(&self, value: T)
pub fn initialize(&self, value: T)
Initialize this InitGuard
, allowing it to be read from in the future.
§Panics
Panics if this InitGuard
has already been initialized.
Sourcepub fn get(&self) -> &T
pub fn get(&self) -> &T
Get a reference to the data, if this guard has been initialized.
§Panics
Panics if this guard hasn’t been initialized yet. Use try_get
if you want a fallible
variant.
Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Get a mutable reference to the data, if this guard has been initialized.
§Panics
Panics if this guard hasn’t been initialized yet. Use try_get_mut
if you want a fallible
variant.
Sourcepub fn try_get(&self) -> Option<&T>
pub fn try_get(&self) -> Option<&T>
Get a reference to the data, if this guard has been initialized. Returns None
if it has
not yet been initialized, or is currently being initialized.
Sourcepub fn try_get_mut(&mut self) -> Option<&mut T>
pub fn try_get_mut(&mut self) -> Option<&mut T>
Get a mutable reference to the data, if this guard has been initialized. Returns None
if it has
not yet been initialized, or is currently being initialized.