Struct metriken::Lazy

source ·
pub struct Lazy<T, F = fn() -> T> { /* private fields */ }
Expand description

A value which is initialized on the first access.

This type is thread-safe and can be used in statics.

§Example

In this example, Heatmap does not have a const new function so it must be constructed using Lazy.

#[metric]
static HEATMAP: Lazy<Heatmap> = Lazy::new(|| {
    Heatmap::new(
        100,
        2,
        Duration::<Nanoseconds<u64>>::from_secs(30),
        Duration::<Nanoseconds<u64>>::from_secs(1),
    )
});

Implementations§

source§

impl<T, F> Lazy<T, F>

source

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

Create a new lazy value with the given initializing function.

source

pub fn get(this: &Self) -> Option<&T>

If this lazy has been initialized, then return a reference to the contained value.

source

pub fn get_mut(this: &mut Self) -> Option<&mut T>

If this lazy has been initialized, then return a reference to the contained value.

source§

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

source

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

Force the evaluation of this lazy value and return a reference to the result. This is equivalent to the Deref impl.

Trait Implementations§

source§

impl<T: Default> Default for Lazy<T>

source§

fn default() -> Self

Create a new lazy value using default as the initializing function.

source§

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

§

type Target = T

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl<T, F: FnOnce() -> T> DerefMut for Lazy<T, F>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T: Metric, F: Send + 'static> Metric for Lazy<T, F>

source§

fn is_enabled(&self) -> bool

Indicate whether this metric has been set up. Read more
source§

fn as_any(&self) -> Option<&dyn Any>

Get the current metric as an Any instance. This is meant to allow custom processing for known metric types.
source§

fn value(&self) -> Option<Value<'_>>

Get the value of the current metric, should it be enabled. Read more
source§

impl<T, F: Send> Sync for Lazy<T, F>
where OnceCell<T>: Sync,

Auto Trait Implementations§

§

impl<T, F = fn() -> T> !Freeze for Lazy<T, F>

§

impl<T, F = fn() -> T> !RefUnwindSafe for Lazy<T, F>

§

impl<T, F> Send for Lazy<T, F>
where T: Send, F: Send,

§

impl<T, F> Unpin for Lazy<T, F>
where F: Unpin, T: Unpin,

§

impl<T, F> UnwindSafe for Lazy<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>,

§

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>,

§

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.