Struct maitake::sync::spin::once::InitOnce

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§

§

impl<T> InitOnce<T>

pub const fn uninitialized() -> InitOnce<T>

Returns a new InitOnce in the uninitialized state.

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.

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.

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.

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.

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.

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§

§

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

§

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

Formats the value using the given formatter. Read more
§

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

§

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

Auto Trait Implementations§

§

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more