Struct embassy_sync::once_lock::OnceLock

source ·
pub struct OnceLock<T> { /* private fields */ }
Expand description

The OnceLock is a synchronization primitive that allows for initializing a value once, and allowing others to .await a reference to the value. This is useful for lazy initialization of a static value.

Note: this implementation uses a busy loop to poll the value, which is not as efficient as registering a dedicated Waker. However, if the usecase for it is to initialize a static variable relatively early in the program life cycle, it should be fine.

§Example

use futures_executor::block_on;
use embassy_sync::once_lock::OnceLock;

// Define a static value that will be lazily initialized
static VALUE: OnceLock<u32> = OnceLock::new();

let f = async {

// Initialize the value
let reference = VALUE.get_or_init(|| 20);
assert_eq!(reference, &20);

// Wait for the value to be initialized
// and get a static reference it
assert_eq!(VALUE.get().await, &20);

};
block_on(f)

Implementations§

source§

impl<T> OnceLock<T>

source

pub const fn new() -> Self

Create a new uninitialized OnceLock.

source

pub async fn get(&self) -> &T

Get a reference to the underlying value, waiting for it to be set. If the value is already set, this will return immediately.

source

pub fn try_get(&self) -> Option<&T>

Try to get a reference to the underlying value if it exists.

source

pub fn init(&self, value: T) -> Result<(), T>

Set the underlying value. If the value is already set, this will return an error with the given value.

source

pub fn get_or_init<F>(&self, f: F) -> &T
where F: FnOnce() -> T,

Get a reference to the underlying value, initializing it if it does not exist.

source

pub fn into_inner(self) -> Option<T>

Consume the OnceLock, returning the underlying value if it was initialized.

source

pub fn take(&mut self) -> Option<T>

Take the underlying value if it was initialized, uninitializing the OnceLock in the process.

source

pub fn is_set(&self) -> bool

Check if the value has been set.

Trait Implementations§

source§

impl<T> Sync for OnceLock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for OnceLock<T>

§

impl<T> !RefUnwindSafe for OnceLock<T>

§

impl<T> Send for OnceLock<T>
where T: Send,

§

impl<T> Unpin for OnceLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for OnceLock<T>
where T: UnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.