Struct Once

Source
pub struct Once { /* private fields */ }
Expand description

A low-level synchronization primitive for one-time global execution.

Useful for one-time initialization for FFI or related functionality. This type can only be constructed with Once::new().

§Examples

use vexide::core::sync::Once;

static START: Once = Once::new();

START.call_once(|| {
    // run initialization here
});

Implementations§

Source§

impl Once

Source

pub const fn new() -> Self

Create a new uncompleted Once

Source

pub fn is_completed(&self) -> bool

Returns true if call_once has been run.

Source

pub async fn call_once<F: FnOnce()>(&self, fun: F)

Runs a closure if and only if this is the first time that call_once or try_call_once has been run.

This is useful for making sure that expensive initialization is only run once. This will block if another task is running a different initialization routine.

Source

pub fn try_call_once<F: FnOnce()>(&self, fun: F) -> Result<(), TryCallOnceError>

Runs a closure if and only if this is the first time that call_once or try_call_once has been run.

Unlike call_once, this function will return an error rather than waiting for initialization.

§Errors

This method will return TryCallOnceError if recursively called during the execution of F. This can only happen if F explicitly tries to call itself, or if using block_on to execute async code from within the function.

Trait Implementations§

Source§

impl Debug for Once

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Once

§

impl !RefUnwindSafe for Once

§

impl Send for Once

§

impl Sync for Once

§

impl Unpin for Once

§

impl UnwindSafe for Once

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.