Struct Lazy
struct Lazy<T, F = fn() -> T>(_)
A lazily initialized value that implements Deref for T.
A Lazy takes an initialization function and permits callers from any
thread to access the result of that initialization function in a safe
manner. In effect, this permits one-time initialization of global resources
in a (possibly) multi-threaded program.
This type and its functionality are available even when neither the alloc
nor the std features are enabled. In exchange, a Lazy does not
guarantee that the given create function is called at most once. It
might be called multiple times. Moreover, a call to Lazy::get (either
explicitly or implicitly via Lazy's Deref impl) may block until a T
is available.
This is very similar to lazy_static or once_cell, except it doesn't
guarantee that the initialization function will be run once and it works
in no-alloc no-std environments. With that said, if you need stronger
guarantees or a more flexible API, then it is recommended to use either
lazy_static or once_cell.
Warning: may use a spin lock
When this crate is compiled without the alloc feature, then this type
may used a spin lock internally. This can have subtle effects that may
be undesirable. See Spinlocks Considered Harmful for a more
thorough treatment of this topic.
Example
This type is useful for creating regexes once, and then using them from multiple threads simultaneously without worrying about synchronization.
use ;
static RE: = new;
let expected = Some;
assert_eq!;
Implementations
impl<T, F> Lazy<T, F>
const fn new(create: F) -> Lazy<T, F>Create a new
Lazyvalue that is initialized via the given function.The
Ttype is automatically inferred from the return type of thecreatefunction given.
impl<T, F: Fn() -> T> Lazy<T, F>
fn get(this: &Lazy<T, F>) -> &TReturn a reference to the lazily initialized value.
This routine may block if another thread is initializing a
T.Note that given a
xwhich has typeLazy, this must be called viaLazy::get(x)and notx.get(). This routine is defined this way becauseLazyimplsDerefwith a target ofT.Panics
This panics if the
createfunction inside this lazy value panics. If the panic occurred in another thread, then this routine may also panic (but is not guaranteed to do so).
impl<P, T> Receiver for Lazy<T, F>
impl<T> Any for Lazy<T, F>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Lazy<T, F>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Lazy<T, F>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for Lazy<T, F>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, F = fn() -> T> Freeze for Lazy<T, F>
impl<T, F> RefUnwindSafe for Lazy<T, F>
impl<T, F> Send for Lazy<T, F>
impl<T, F> Sync for Lazy<T, F>
impl<T, F> Unpin for Lazy<T, F>
impl<T, F> UnsafeUnpin for Lazy<T, F>
impl<T, F> UnwindSafe for Lazy<T, F>
impl<T, F: Fn() -> T> Deref for Lazy<T, F>
fn deref(self: &Self) -> &T
impl<T, U> Into for Lazy<T, F>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Lazy<T, F>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Lazy<T, F>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: fmt::Debug, F: Fn() -> T> Debug for Lazy<T, F>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result