maitake_sync::spin::once

Struct InitOnce

Source
pub struct InitOnce<T> { /* private fields */ }
Expand description

A cell which may be initialized a single time after it is created.

This can be used as a safer alternative to static mut.

For performance-critical use-cases, this type also has a get_unchecked method, which dereferences the cell without checking if it has been initialized. This method is unsafe and should be used with caution — incorrect usage can result in reading uninitialized memory.

Implementations§

Source§

impl<T> InitOnce<T>

Source

pub const fn uninitialized() -> Self

Returns a new InitOnce in the uninitialized state.

Source

pub fn try_init(&self, value: T) -> Result<(), TryInitError<T>>

Initialize the cell to value, returning an error if it has already been initialized.

If the cell has already been initialized, the returned error contains the value.

Source

pub fn init(&self, value: T) -> &T

Initialize the cell to value, panicking if it has already been initialized.

§Panics

If the cell has already been initialized.

Source

pub fn try_get(&self) -> Option<&T>

Borrow the contents of this InitOnce cell, if it has been initialized. Otherwise, if the cell has not yet been initialized, this returns None.

Source

pub fn get(&self) -> &T

Borrow the contents of this InitOnce cell, or panic if it has not been initialized.

§Panics

If the cell has not yet been initialized.

Source

pub fn get_or_else(&self, f: impl FnOnce() -> T) -> &T

Borrow the contents of this InitOnce cell, or initialize it with the provided closure.

If the cell has been initialized, this returns the current value. Otherwise, it calls the closure, puts the returned value from the closure in the cell, and borrows the current value.

Source

pub unsafe fn get_unchecked(&self) -> &T

Borrow the contents of this InitOnce cell, without checking whether it has been initialized.

§Safety

The caller is responsible for ensuring that the value has already been initialized.

In debug mode, this still checks the state of the cell, so if it has yet to be initialized, this will panic. However, in release mode builds, this is completely unchecked. If the value has not yet been initialized, this may return a pointer to uninitialized memory! It may also return a pointer to memory that is currently being written to.

If you see this method panic in debug mode, please, please re-check your code.

Trait Implementations§

Source§

impl<T: Debug> Debug for InitOnce<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Send> Send for InitOnce<T>

Source§

impl<T: Sync> Sync for InitOnce<T>

Auto Trait Implementations§

§

impl<T> !Freeze for InitOnce<T>

§

impl<T> !RefUnwindSafe for InitOnce<T>

§

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

§

impl<T> UnwindSafe for InitOnce<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.