pub struct LazyLock<T, I = fn() -> T> { /* private fields */ }
Expand description
A thread-safe value which is initialized on first access.
This type is a thread-safe LazyCell
, and can be used in statics.
§Differences from std::sync::LazyLock
There are two possible edge cases that can cause different behavior in this type
compared to its std
counterpart:
- If the type is lazily initialized from within its own initialization function, a panic will occur rather than an infinite deadlock.
- By extension, if the initialization function uses
block_on
to executeasync
code and another task attempts to access the underlying value of this type (through either dereferencing or usingLazyLock::force
) before the initialization function has returned a value, this function will panic rather than block. As such, you should generally avoid usingblock_on
when creating a value inside of aLazyLock
.
These two differences allow us to implement LazyLock
without an actual lock at all, since
we guarantee exclusive access after initialization due to the V5 being single-threaded.
Implementations§
Source§impl<T, I: FnOnce() -> T> LazyLock<T, I>
impl<T, I: FnOnce() -> T> LazyLock<T, I>
Sourcepub fn into_inner(self) -> Result<T, I>
pub fn into_inner(self) -> Result<T, I>
Sourcepub fn force(&self) -> &T
pub fn force(&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.
§Panics
This method will panic under two possible edge-cases:
- It is called recursively from within its own initialization function.
- The initialization function uses
block_on
to executeasync
code, and another task attempts to access the underlying value of this type in the middle of it lazily initializing.
This behavior differs from the standard library, which would normally either block the current thread or deadlock forever. Since the V5 brain is a single-core system, it was determined that panicking is a more acceptable compromise than an unrecoverable deadlock.
Sourcepub fn force_mut(&mut self) -> &mut T
pub fn force_mut(&mut self) -> &mut T
Forces the evaluation of this lazy value and returns a mutable reference to
the result. This is equivalent to the DerefMut
impl, but is explicit.
§Panics
This method will panic under two possible edge-cases:
- It is called recursively from within its own initialization function.
- The initialization function uses
block_on
to executeasync
code, and another task attempts to access the underlying value of this type in the middle of it lazily initializing.
This behavior differs from the standard library, which would normally either block the current thread or deadlock forever. Since the V5 brain is a single-core system, it was determined that panicking is a more acceptable compromise than an unrecoverable deadlock.
Trait Implementations§
Source§impl<T, I: FnOnce() -> T> Deref for LazyLock<T, I>
impl<T, I: FnOnce() -> T> Deref for LazyLock<T, I>
Source§fn deref(&self) -> &Self::Target
fn deref(&self) -> &Self::Target
Dereferences the value.
§Panics
This method will panic under two possible edge-cases:
- It is called recursively from within its own initialization function.
- The initialization function uses
block_on
to executeasync
code, and another task attempts to access the underlying value of this type in the middle of it lazily initializing.
This behavior differs from the standard library, which would normally either block the current thread or deadlock forever. Since the V5 brain is a single-core system, it was determined that panicking is a more acceptable compromise than an unrecoverable deadlock.
Source§impl<T, I: FnOnce() -> T> DerefMut for LazyLock<T, I>
impl<T, I: FnOnce() -> T> DerefMut for LazyLock<T, I>
Source§fn deref_mut(&mut self) -> &mut Self::Target
fn deref_mut(&mut self) -> &mut Self::Target
Mutably dereferences the value.
§Panics
This method will panic under two possible edge-cases:
- It is called recursively from within its own initialization function.
- The initialization function uses
block_on
to executeasync
code, and another task attempts to access the underlying value of this type in the middle of it lazily initializing.
This behavior differs from the standard library, which would normally either block the current thread or deadlock forever. Since the V5 brain is a single-core system, it was determined that panicking is a more acceptable compromise than an unrecoverable deadlock.