pub struct OnceCell<T> {
value: UnsafeCell<MaybeUninit<T>>,
init: Ready,
}Expand description
A synchronization primitive that represents a value that is initialized at most once.
Fields§
§value: UnsafeCell<MaybeUninit<T>>§init: ReadyImplementations§
Source§impl<T> OnceCell<T>
impl<T> OnceCell<T>
Sourcepub fn set_or_get(&self, value: T) -> &T
pub fn set_or_get(&self, value: T) -> &T
Sets the value if it is not already initialized, and returns a reference to the value.
Sourcepub fn do_or_get<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
pub fn do_or_get<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
Sets the value if it is not already initialized, and returns a reference to the value.
Sourcepub fn set(&self, value: T) -> Option<&T>
pub fn set(&self, value: T) -> Option<&T>
Sets the value if it is not already initialized, returns a reference to the value if it was not set previously.
Sourceunsafe fn get_unchecked(&self) -> &T
unsafe fn get_unchecked(&self) -> &T
Returns a reference to the value, unchecked.
§Safety
Preconditions: The value must be initialized. Postconditions: The value is returned.
Trait Implementations§
impl<T> Sync for OnceCell<T>
Safety:
- A
valueis only written to atomically and once. - A
valueis only readable from after the initialization process is finished. - A
initis only written and read from atomically.
Auto Trait Implementations§
impl<T> !Freeze for OnceCell<T>
impl<T> !RefUnwindSafe for OnceCell<T>
impl<T> Send for OnceCell<T>where
T: Send,
impl<T> Unpin for OnceCell<T>where
T: Unpin,
impl<T> UnwindSafe for OnceCell<T>where
T: UnwindSafe,
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