Struct cucumber::codegen::Lazy

source ·
pub struct Lazy<T, F = fn() -> T> { /* private fields */ }
Available on crate feature macros only.
Expand description

A value which is initialized on the first access.

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

Example

use std::collections::HashMap;

use once_cell::sync::Lazy;

static HASHMAP: Lazy<HashMap<i32, String>> = Lazy::new(|| {
    println!("initializing");
    let mut m = HashMap::new();
    m.insert(13, "Spica".to_string());
    m.insert(74, "Hoyten".to_string());
    m
});

fn main() {
    println!("ready");
    std::thread::spawn(|| {
        println!("{:?}", HASHMAP.get(&13));
    }).join().unwrap();
    println!("{:?}", HASHMAP.get(&74));

    // Prints:
    //   ready
    //   initializing
    //   Some("Spica")
    //   Some("Hoyten")
}

Implementations

Creates a new lazy value with the given initializing function.

Consumes this Lazy returning the stored value.

Returns Ok(value) if Lazy is initialized and Err(f) otherwise.

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

Example
use once_cell::sync::Lazy;

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

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

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

Example
use once_cell::sync::Lazy;

let mut lazy = Lazy::new(|| 92);

assert_eq!(Lazy::force_mut(&mut lazy), &mut 92);

Gets the reference to the result of this lazy value if it was initialized, otherwise returns None.

Example
use once_cell::sync::Lazy;

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

assert_eq!(Lazy::get(&lazy), None);
assert_eq!(&*lazy, &92);
assert_eq!(Lazy::get(&lazy), Some(&92));

Gets the reference to the result of this lazy value if it was initialized, otherwise returns None.

Example
use once_cell::sync::Lazy;

let mut lazy = Lazy::new(|| 92);

assert_eq!(Lazy::get_mut(&mut lazy), None);
assert_eq!(&*lazy, &92);
assert_eq!(Lazy::get_mut(&mut lazy), Some(&mut 92));

Trait Implementations

Formats the value using the given formatter. Read more

Creates a new lazy value using Default as the initializing function.

The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Asserts this Writer being Normalized. Read more
Wraps this Writer into a Normalized version. Read more
Wraps this Writer to print a summary at the end of an output. Read more
Wraps this Writer to fail on Skipped Steps if their Scenario isn’t marked with @allow.skipped tag. Read more
Wraps this Writer to fail on Skipped Steps if the given with predicate returns true. Read more
Wraps this Writer to re-output Skipped Steps at the end of an output. Read more
Wraps this Writer to re-output Failed Steps or Parser errors at the end of an output. Read more
Wraps this Writer to re-output filtered events at the end of an output. Read more
Attaches the provided other Writer to the current one for passing events to both of them simultaneously. Read more
Wraps this Writer into a discard::Arbitrary one, providing a no-op ArbitraryWriter implementation. Read more
Wraps this Writer into a discard::Stats one, providing a no-op StatsWriter implementation returning only 0. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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