Struct tokio::sync::OnceCell [−][src]
pub struct OnceCell<T> { /* fields omitted */ }
sync
only.Expand description
A thread-safe cell which can be written to only once.
Provides the functionality to either set the value, in case OnceCell
is uninitialized, or get the already initialized value by using an async
function via OnceCell::get_or_init
.
Examples
use tokio::sync::OnceCell; async fn some_computation() -> u32 { 1 + 1 } static ONCE: OnceCell<u32> = OnceCell::const_new(); #[tokio::main] async fn main() { let result1 = ONCE.get_or_init(some_computation).await; assert_eq!(*result1, 2); }
Implementations
impl<T> OnceCell<T>
[src]
impl<T> OnceCell<T>
[src]pub fn new_with(value: Option<T>) -> Self
[src]
pub fn new_with(value: Option<T>) -> Self
[src]Creates a new initialized OnceCell instance if value
is Some
, otherwise
has the same functionality as OnceCell::new
.
pub const fn const_new() -> Self
[src]
This is supported on crate feature parking_lot
only.
pub const fn const_new() -> Self
[src]parking_lot
only.Creates a new uninitialized OnceCell instance.
pub fn initialized(&self) -> bool
[src]
pub fn initialized(&self) -> bool
[src]Whether the value of the OnceCell is set or not.
pub fn get(&self) -> Option<&T>
[src]
pub fn get(&self) -> Option<&T>
[src]Tries to get a reference to the value of the OnceCell.
Returns None if the value of the OnceCell hasn’t previously been initialized.
pub fn get_mut(&mut self) -> Option<&mut T>
[src]
pub fn get_mut(&mut self) -> Option<&mut T>
[src]Tries to return a mutable reference to the value of the cell.
Returns None if the cell hasn’t previously been initialized.
pub fn set(&self, value: T) -> Result<(), SetError<T>>
[src]
pub fn set(&self, value: T) -> Result<(), SetError<T>>
[src]Sets the value of the OnceCell to the argument value.
If the value of the OnceCell was already set prior to this call
then SetError::AlreadyInitializedError
is returned. If another thread
is initializing the cell while this method is called,
SetError::InitializingError
is returned. In order to wait
for an ongoing initialization to finish, call
OnceCell::get_or_init
instead.
pub async fn get_or_init<F, Fut>(&self, f: F) -> &T where
F: FnOnce() -> Fut,
Fut: Future<Output = T>,
[src]
pub async fn get_or_init<F, Fut>(&self, f: F) -> &T where
F: FnOnce() -> Fut,
Fut: Future<Output = T>,
[src]Tries to initialize the value of the OnceCell using the async function f
.
If the value of the OnceCell was already initialized prior to this call,
a reference to that initialized value is returned. If some other thread
initiated the initialization prior to this call and the initialization
hasn’t completed, this call waits until the initialization is finished.
This will deadlock if f
tries to initialize the cell itself.
pub async fn get_or_try_init<E, F, Fut>(&self, f: F) -> Result<&T, E> where
F: FnOnce() -> Fut,
Fut: Future<Output = Result<T, E>>,
[src]
pub async fn get_or_try_init<E, F, Fut>(&self, f: F) -> Result<&T, E> where
F: FnOnce() -> Fut,
Fut: Future<Output = Result<T, E>>,
[src]Tries to initialize the value of the OnceCell using the async function f
.
If the value of the OnceCell was already initialized prior to this call,
a reference to that initialized value is returned. If some other thread
initiated the initialization prior to this call and the initialization
hasn’t completed, this call waits until the initialization is finished.
If the function argument f
returns an error, get_or_try_init
returns that error, otherwise the result of f
will be stored in the cell.
This will deadlock if f
tries to initialize the cell itself.
pub fn into_inner(self) -> Option<T>
[src]
pub fn into_inner(self) -> Option<T>
[src]Moves the value out of the cell, destroying the cell in the process.
Returns None
if the cell is uninitialized.
Trait Implementations
impl<T: Eq> Eq for OnceCell<T>
[src]
impl<T: Send> Send for OnceCell<T>
[src]
impl<T: Sync + Send> Sync for OnceCell<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for OnceCell<T>
impl<T> Unpin for OnceCell<T> where
T: Unpin,
T: Unpin,
impl<T> !UnwindSafe for OnceCell<T>
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more