mulch

Struct InitGuard

Source
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>

Source

pub const fn uninit() -> InitGuard<T>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<T: Send> Send for InitGuard<T>

Source§

impl<T: Send + Sync> Sync for InitGuard<T>

Auto Trait Implementations§

§

impl<T> !Freeze for InitGuard<T>

§

impl<T> !RefUnwindSafe for InitGuard<T>

§

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

§

impl<T> UnwindSafe for InitGuard<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>,

Source§

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>,

Source§

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.