OnceInit

Struct OnceInit 

Source
pub struct OnceInit<T: ?Sized + 'static>{ /* private fields */ }
Expand description

§OnceInit

仅可设置一次数据的类型。

T 实现了 Sync 时,该类型也会实现 Sync. Sync 是由内部原子类型的 state 和外部 api 共同保证的。 外部 api 保证,当 state 指示数据正在或已经初始化时,该类型不可变。

Implementations§

Source§

impl<T: ?Sized> OnceInit<T>

Source

pub const fn uninit() -> Self

返回未初始化的 OnceInit 类型。

Source

pub const fn new(data: &'static T) -> Self
where &'static T: Sized, Self: Sized,

返回初始化过的 OnceInit 类型。

Source

pub fn get(&self) -> Result<&'static T, OnceInitError>

返回内部数据,若未初始化,则返回 OnceInitError.

若需要可变数据,请在内部使用具有内部可见性的数据结构,如 Mutex 等。

Source

pub fn get_or_default(&self) -> &'static T
where T: StaticDefault,

返回内部数据,若未初始化,则返回 <T as StaticDefault>::static_default().

需要 T 实现 StaticDefault.

Source

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

不检查是否初始化,直接返回内部数据。

若需要可变数据,请在内部使用具有内部可见性的数据结构,如 Mutex 等。

§Safety

未初始化时,调用此函数会在内部的 None 值上调用 Option::unwrap_unchecked, 造成未定义行为

Source

pub fn state(&self) -> OnceInitState

返回数据状态,见 OnceInitState.

Source

pub fn init(&self, data: &'static T) -> Result<(), OnceInitError>

初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。

如果 data 不是 'static 的,请使用 init_boxed.

Source

pub fn init_boxed(&self, data: Box<T>) -> Result<(), OnceInitError>

初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。

Trait Implementations§

Source§

impl<T: ?Sized + Debug> Debug for OnceInit<T>

Source§

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

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

impl<T> Default for OnceInit<T>
where T: ?Sized + StaticDefault, Self: Sized,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ?Sized + StaticDefault> Deref for OnceInit<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &'static Self::Target

Dereferences the value.
Source§

impl<T: ?Sized> UninitGlobalHolder<T> for OnceInit<T>

Source§

fn init(&self, data: &'static T) -> Result<(), OnceInitError>

初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。

如果 data 不是 'static 的,请使用 init_boxed.

Source§

fn init_boxed(&self, data: Box<T>) -> Result<(), OnceInitError>

初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。

Source§

impl<T> Sync for OnceInit<T>
where T: ?Sized + Sync,

Auto Trait Implementations§

§

impl<T> !Freeze for OnceInit<T>

§

impl<T> !RefUnwindSafe for OnceInit<T>

§

impl<T> Send for OnceInit<T>
where &'static T: Sized, T: Sync + ?Sized,

§

impl<T> Unpin for OnceInit<T>
where &'static T: Sized, T: ?Sized,

§

impl<T> UnwindSafe for OnceInit<T>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.