pub struct OnceInit<T: ?Sized + 'static>where
&'static T: Sized,{ /* private fields */ }Expand description
Implementations§
Source§impl<T: ?Sized> OnceInit<T>
impl<T: ?Sized> OnceInit<T>
Sourcepub const fn new(data: &'static T) -> Self
pub const fn new(data: &'static T) -> Self
返回初始化过的 OnceInit 类型。
Sourcepub fn get(&self) -> Result<&'static T, OnceInitError>
pub fn get(&self) -> Result<&'static T, OnceInitError>
返回内部数据,若未初始化,则返回 OnceInitError.
若需要可变数据,请在内部使用具有内部可见性的数据结构,如 Mutex 等。
Sourcepub fn get_or_default(&self) -> &'static Twhere
T: StaticDefault,
pub fn get_or_default(&self) -> &'static Twhere
T: StaticDefault,
返回内部数据,若未初始化,则返回 <T as StaticDefault>::static_default().
需要 T 实现 StaticDefault.
Sourcepub unsafe fn get_unchecked(&self) -> &'static T
pub unsafe fn get_unchecked(&self) -> &'static T
不检查是否初始化,直接返回内部数据。
若需要可变数据,请在内部使用具有内部可见性的数据结构,如 Mutex 等。
§Safety
未初始化时,调用此函数会在内部的 None 值上调用 Option::unwrap_unchecked, 造成未定义行为。
Sourcepub fn state(&self) -> OnceInitState
pub fn state(&self) -> OnceInitState
返回数据状态,见 OnceInitState.
Sourcepub fn init(&self, data: &'static T) -> Result<(), OnceInitError>
pub fn init(&self, data: &'static T) -> Result<(), OnceInitError>
初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。
如果 data 不是 'static 的,请使用 init_boxed.
Sourcepub fn init_boxed(&self, data: Box<T>) -> Result<(), OnceInitError>
pub fn init_boxed(&self, data: Box<T>) -> Result<(), OnceInitError>
初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。
Trait Implementations§
Source§impl<T: ?Sized> UninitGlobalHolder<T> for OnceInit<T>
impl<T: ?Sized> UninitGlobalHolder<T> for OnceInit<T>
Source§fn init(&self, data: &'static T) -> Result<(), OnceInitError>
fn init(&self, data: &'static T) -> Result<(), OnceInitError>
初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。
如果 data 不是 'static 的,请使用 init_boxed.
Source§fn init_boxed(&self, data: Box<T>) -> Result<(), OnceInitError>
fn init_boxed(&self, data: Box<T>) -> Result<(), OnceInitError>
初始化内部数据,只可调用一次,成功则初始化完成,之后调用均会返回错误。
impl<T> Sync for OnceInit<T>
Auto Trait Implementations§
impl<T> !Freeze for OnceInit<T>
impl<T> !RefUnwindSafe for OnceInit<T>
impl<T> Send for OnceInit<T>
impl<T> Unpin for OnceInit<T>
impl<T> UnwindSafe for OnceInit<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more