pub struct Lazy<T> { /* private fields */ }Expand description
Lazy is a thread-safe, lazily-initialized global variable.
Unlike other async once-cell implementations, accessing the value of a Lazy instance is synchronous
and done on deref.
This is done by offloading the async initialization to a blocking thread during the first access, and then using the initialized value for all subsequent accesses.
It uses std::sync::OnceLock internally to ensure that the value is only initialized once.
Implementations§
Source§impl<T: Send + Sync + 'static> Lazy<T>
impl<T: Send + Sync + 'static> Lazy<T>
Sourcepub const fn lazy() -> Self
pub const fn lazy() -> Self
Create a new Lazy instance.
This internally calls std::sync::OnceLock::new() under the hood.
pub const fn new<F, G, E>(constructor: F) -> Self
Sourcepub fn set(&self, pool: T) -> Result<(), CapturedError>
pub fn set(&self, pool: T) -> Result<(), CapturedError>
Set the value of the Lazy instance.
This should only be called once during the server setup phase, typically inside dioxus::serve.
Future calls to this method will return an error containing the provided value.
pub fn try_set(&self, pool: T) -> Result<(), T>
Sourcepub fn initialize(&self) -> Result<(), CapturedError>
pub fn initialize(&self) -> Result<(), CapturedError>
Initialize the value of the Lazy instance if it hasn’t been initialized yet.