Struct SetOnce
pub struct SetOnce<T> { /* private fields */ }
A thread-safe cell that can be written to only once.
A SetOnce is inspired from python's asyncio.Event type. It can be
used to wait until the value of the SetOnce is set like a "Event" mechanism.
Example
use ;
static ONCE: = const_new;
#
# async
A SetOnce is typically used for global variables that need to be
initialized once on first use, but need no further changes. The SetOnce
in Tokio allows the initialization procedure to be asynchronous.
Example
use ;
use Arc;
#
# async
Implementations
impl<T> SetOnce<T>
fn new() -> SelfCreates a new empty
SetOnceinstance.const fn const_new() -> SelfCreates a new empty
SetOnceinstance.Equivalent to
SetOnce::new, except that it can be used in static variables.When using the
tracingunstable feature, aSetOncecreated withconst_newwill not be instrumented. As such, it will not be visible intokio-console. Instead,SetOnce::newshould be used to create an instrumented object if that is needed.Example
use ; static ONCE: = const_new; # # asyncfn new_with(value: Option<T>) -> SelfCreates a new
SetOncethat contains the provided value, if any.If the
OptionisNone, this is equivalent toSetOnce::new.const fn const_new_with(value: T) -> SelfCreates a new
SetOncethat contains the provided value.Example
When using the
tracingunstable feature, aSetOncecreated withconst_new_withwill not be instrumented. As such, it will not be visible intokio-console. Instead,SetOnce::new_withshould be used to create an instrumented object if that is needed.use SetOnce; static ONCE: = const_new_with; # # asyncfn initialized(&self) -> boolReturns
trueif theSetOncecurrently contains a value, andfalseotherwise.fn get(&self) -> Option<&T>Returns a reference to the value currently stored in the
SetOnce, orNoneif theSetOnceis empty.fn set(&self, value: T) -> Result<(), SetOnceError<T>>Sets the value of the
SetOnceto the given value if theSetOnceis empty.If the
SetOncealready has a value, this call will fail with anSetOnceError.fn into_inner(self) -> Option<T>Takes the value from the cell, destroying the cell in the process. Returns
Noneif the cell is empty.async fn wait(&self) -> &TWaits until the value is set.
If the
SetOnceis already initialized, it will return the value immediately.Cancel safety
This method is cancel safe.
Trait Implementations
impl<T> Default for SetOnce<T>
fn default() -> SetOnce<T>
impl<T> Drop for SetOnce<T>
fn drop(&mut self)
impl<T> From<T> for SetOnce<T>
fn from(value: T) -> Self
impl<T: Clone> Clone for SetOnce<T>
fn clone(&self) -> SetOnce<T>
impl<T: Debug> Debug for SetOnce<T>
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result
impl<T: Eq> Eq for SetOnce<T>
impl<T: PartialEq> PartialEq for SetOnce<T>
fn eq(&self, other: &SetOnce<T>) -> bool
impl<T: Send> Send for SetOnce<T>
impl<T: Sync + Send> Sync for SetOnce<T>
Auto Trait Implementations
impl<T> !Freeze for SetOnce<T>
impl<T> !RefUnwindSafe for SetOnce<T>
impl<T> Unpin for SetOnce<T>
where
UnsafeCell<MaybeUninit<T>>: Unpin,
impl<T> UnsafeUnpin for SetOnce<T>
where
UnsafeCell<MaybeUninit<T>>: UnsafeUnpin,
impl<T> UnwindSafe for SetOnce<T>
where
UnsafeCell<MaybeUninit<T>>: UnwindSafe,
Blanket Implementations
impl<T> Any for SetOnce<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for SetOnce<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for SetOnce<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for SetOnce<T>
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for SetOnce<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for SetOnce<T>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for SetOnce<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for SetOnce<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for SetOnce<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>