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
impl Once
Sourcepub fn is_completed(&self) -> bool
pub fn is_completed(&self) -> bool
Returns true if call_once has been run.
Sourcepub async fn call_once<F: FnOnce()>(&self, fun: F)
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.
Sourcepub fn try_call_once<F: FnOnce()>(&self, fun: F) -> Result<(), TryCallOnceError>
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.