embassy_sync::once_lock

Struct 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§

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.