embassy_sync::lazy_lock

Struct LazyLock

Source
pub struct LazyLock<T, F = fn() -> T> { /* private fields */ }
Expand description

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

§Example

use futures_executor::block_on;
use embassy_sync::lazy_lock::LazyLock;

// Define a static value that will be lazily initialized
// at runtime at the first access.
static VALUE: LazyLock<u32> = LazyLock::new(|| 20);

let reference = VALUE.get();
assert_eq!(reference, &20);

Implementations§

Source§

impl<T, F: FnOnce() -> T> LazyLock<T, F>

Source

pub const fn new(init_fn: F) -> Self

Create a new uninitialized StaticLock.

Source

pub fn get(&self) -> &T

Get a reference to the underlying value, initializing it if it has not been done already.

Source

pub fn into_inner(self) -> T

Consume the LazyLock, returning the underlying value. The initialization function will be called if it has not been already.

Trait Implementations§

Source§

impl<T, F> Drop for LazyLock<T, F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, F> Sync for LazyLock<T, F>

Auto Trait Implementations§

§

impl<T, F = fn() -> T> !Freeze for LazyLock<T, F>

§

impl<T, F = fn() -> T> !RefUnwindSafe for LazyLock<T, F>

§

impl<T, F> Send for LazyLock<T, F>
where T: Send, F: Send,

§

impl<T, F> Unpin for LazyLock<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnwindSafe for LazyLock<T, F>
where T: UnwindSafe, F: 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.