pub struct KOnceCell<T, Class> { /* private fields */ }Expand description
A cell that provides lazy/one-time initialization synchronized by a lock class Class.
KOnceCell holds data of type T that is uninitialized when created, and can be initialized
at most once by presenting proof (LockToken) of holding a lock of class Class.
Because access is token-gated by Class, initialization is completely free of atomic overhead.
Implementations§
Source§impl<T, Class> KOnceCell<T, Class>
impl<T, Class> KOnceCell<T, Class>
Sourcepub unsafe fn is_initialized(&self, _token: &LockToken<'_, Class>) -> bool
pub unsafe fn is_initialized(&self, _token: &LockToken<'_, Class>) -> bool
Returns true if the cell has been initialized.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Sourcepub unsafe fn get<'b>(&self, _token: &'b LockToken<'_, Class>) -> Option<&'b T>
pub unsafe fn get<'b>(&self, _token: &'b LockToken<'_, Class>) -> Option<&'b T>
Accesses the initialized value immutably using a shared lock token.
Returns None if not yet initialized.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Sourcepub unsafe fn get_mut<'b>(
&self,
_token: &'b mut LockToken<'_, Class>,
) -> Option<&'b mut T>
pub unsafe fn get_mut<'b>( &self, _token: &'b mut LockToken<'_, Class>, ) -> Option<&'b mut T>
Accesses the initialized value mutably using a mutable lock token.
Returns None if not yet initialized.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Sourcepub unsafe fn set(
&self,
value: T,
_token: &mut LockToken<'_, Class>,
) -> Result<(), T>
pub unsafe fn set( &self, value: T, _token: &mut LockToken<'_, Class>, ) -> Result<(), T>
Sets the value of the cell if uninitialized, returning Err(value) if already initialized.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Sourcepub unsafe fn get_or_init<'b>(
&self,
f: impl FnOnce() -> T,
_token: &'b mut LockToken<'_, Class>,
) -> &'b mut T
pub unsafe fn get_or_init<'b>( &self, f: impl FnOnce() -> T, _token: &'b mut LockToken<'_, Class>, ) -> &'b mut T
Initializes the cell with the given closure if uninitialized, returning a mutable reference to the contained value.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Sourcepub unsafe fn get_or_try_init<'b, E>(
&self,
f: impl FnOnce() -> Result<T, E>,
_token: &'b mut LockToken<'_, Class>,
) -> Result<&'b mut T, E>
pub unsafe fn get_or_try_init<'b, E>( &self, f: impl FnOnce() -> Result<T, E>, _token: &'b mut LockToken<'_, Class>, ) -> Result<&'b mut T, E>
Initializes the cell with the given fallible closure if uninitialized, returning a mutable reference to the contained value or the error.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Sourcepub unsafe fn get_or_pin_init<'b, E>(
&self,
init: impl PinInit<T, E>,
_token: &'b mut LockToken<'_, Class>,
) -> Result<&'b mut T, E>
pub unsafe fn get_or_pin_init<'b, E>( &self, init: impl PinInit<T, E>, _token: &'b mut LockToken<'_, Class>, ) -> Result<&'b mut T, E>
Initializes the cell in-place with PinInit if uninitialized, returning a mutable reference
to the contained value or the initialization error.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Sourcepub unsafe fn get_unchecked(&self) -> Option<&T>
pub unsafe fn get_unchecked(&self) -> Option<&T>
Accesses the initialized value immutably without checking for a LockToken.
Returns None if not yet initialized.
§Safety
The caller must guarantee that the specific lock instance guarding this KOnceCell is held.
Sourcepub unsafe fn get_mut_unchecked(&self) -> Option<&mut T>
pub unsafe fn get_mut_unchecked(&self) -> Option<&mut T>
Accesses the initialized value mutably without checking for a LockToken.
Returns None if not yet initialized.
§Safety
The caller must guarantee that the specific lock instance guarding this KOnceCell is held
exclusively without concurrent access.
Sourcepub unsafe fn get_or_init_unchecked(&self, f: impl FnOnce() -> T) -> &mut T
pub unsafe fn get_or_init_unchecked(&self, f: impl FnOnce() -> T) -> &mut T
Initializes the cell with the given closure without requiring a LockToken.
§Safety
The caller must guarantee that the specific lock instance guarding this KOnceCell is held
exclusively without concurrent access.
Sourcepub unsafe fn get_or_try_init_unchecked<E>(
&self,
f: impl FnOnce() -> Result<T, E>,
) -> Result<&mut T, E>
pub unsafe fn get_or_try_init_unchecked<E>( &self, f: impl FnOnce() -> Result<T, E>, ) -> Result<&mut T, E>
Initializes the cell with the given fallible closure without requiring a LockToken.
§Safety
The caller must guarantee that the specific lock instance guarding this KOnceCell is held
exclusively without concurrent access.
Sourcepub unsafe fn get_or_pin_init_unchecked<E>(
&self,
init: impl PinInit<T, E>,
) -> Result<&mut T, E>
pub unsafe fn get_or_pin_init_unchecked<E>( &self, init: impl PinInit<T, E>, ) -> Result<&mut T, E>
Initializes the cell in-place with PinInit without requiring a LockToken.
§Safety
The caller must guarantee that the specific lock instance guarding this KOnceCell is held
exclusively without concurrent access.
Sourcepub fn get_inner_mut(&mut self) -> Option<&mut T>
pub fn get_inner_mut(&mut self) -> Option<&mut T>
Accesses the inner value mutably by bypassing locking requirements using unique borrow ownership.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Unwraps the cell, returning the inner value if initialized.
Sourcepub unsafe fn guard<'a>(
&'a self,
token: &'a mut LockToken<'_, Class>,
) -> KOnceCellGuard<'a, T, Class>
pub unsafe fn guard<'a>( &'a self, token: &'a mut LockToken<'_, Class>, ) -> KOnceCellGuard<'a, T, Class>
Returns a safe guard proxy for this cell using an exclusive lock token.
§Safety
The caller must guarantee that the provided LockToken belongs to the specific lock
instance that guards this KOnceCell (rather than a different lock of the same lock class
Class).
Trait Implementations§
impl<T: Send, Class> Send for KOnceCell<T, Class>
impl<T: Send, Class> Sync for KOnceCell<T, Class>
Auto Trait Implementations§
impl<T, Class> !Freeze for KOnceCell<T, Class>
impl<T, Class> !RefUnwindSafe for KOnceCell<T, Class>
impl<T, Class> Unpin for KOnceCell<T, Class>where
Class: Unpin,
T: Unpin,
impl<T, Class> UnsafeUnpin for KOnceCell<T, Class>where
T: UnsafeUnpin,
impl<T, Class> UnwindSafe for KOnceCell<T, Class>where
Class: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere
U: From<T>,
Source§impl<T> PinInit<T> for T
impl<T> PinInit<T> for T
Source§unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), !>
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), !>
slot. Read moreSource§fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>where
F: FnOnce(Pin<&mut T>) -> Result<(), E>,
fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>where
F: FnOnce(Pin<&mut T>) -> Result<(), E>,
self then calls the function f with the initialized
value. Read more