embassy-sync

Crates

git

Versions

default

Flavors

Struct embassy_sync::lazy_lock::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.