Skip to main content

LazyLock

Struct LazyLock 

Source
pub struct LazyLock<T, F = fn() -> T> { /* private fields */ }
Available on crate feature full and non-crate feature use_std only.
Expand description

A value which is initialized on the first access.

This type is a thread-safe LazyCell, and can be used in statics. Since initialization may be called from multiple threads, any dereferencing call will block the calling thread if another initialization routine is currently running.

§Poisoning

If the initialization closure passed to LazyLock::new panics, the lock will be poisoned. Once the lock is poisoned, any threads that attempt to access this lock (via a dereference or via an explicit call to force()) will panic.

§Examples

Initialize static variables with LazyLock.

use kernel_api::sync::LazyLock;

// Note: static items do not call [`Drop`] on program termination, so this won't be deallocated.
static DEEP_THOUGHT: LazyLock<String> = LazyLock::new(|| {
    another_crate::great_question()
});

// The `String` is built, stored in the `LazyLock`, and returned as `&String`.
let _ = &*DEEP_THOUGHT;

Initialize fields with LazyLock.

use kernel_api::sync::LazyLock;

#[derive(Debug)]
struct UseCellLock {
    number: LazyLock<u32>,
}
fn main() {
    let lock: LazyLock<u32> = LazyLock::new(|| 0u32);

    let data = UseCellLock { number: lock };
    assert_eq!(*data.number, 0);
}

Implementations§

Source§

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

Source

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

Creates a new lazy value with the given initializing function.

§Examples
use kernel_api::sync::LazyLock;

let hello = "Hello, World!".to_string();

let lazy = LazyLock::new(|| hello.to_uppercase());

assert_eq!(&*lazy, "HELLO, WORLD!");
Source

pub fn force(this: &Self) -> &T

Forces the evaluation of this lazy value and returns a reference to result. This is equivalent to the Deref impl, but is explicit.

This method will block the calling thread if another initialization routine is currently running.

§Panics

If the initialization closure panics (the one that is passed to the new() method), the panic is propagated to the caller, and the lock becomes poisoned. This will cause all future accesses of the lock (via force() or a dereference) to panic.

§Examples
use kernel_api::sync::LazyLock;

let lazy = LazyLock::new(|| 92);

assert_eq!(LazyLock::force(&lazy), &92);
assert_eq!(&*lazy, &92);

Trait Implementations§

Source§

impl<T: Debug, F: Debug> Debug for LazyLock<T, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: Sync + Send, F: Send> 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 OnceLock<T>: Send, MaybeUninit<F>: Send,

§

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

§

impl<T, F> UnsafeUnpin for LazyLock<T, F>
where OnceLock<T>: UnsafeUnpin, MaybeUninit<F>: UnsafeUnpin,

§

impl<T, F> UnwindSafe for LazyLock<T, F>
where OnceLock<T>: UnwindSafe, MaybeUninit<F>: 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
§

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

§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.