Function moxie::once[][src]

pub fn once<Output>(init: impl FnOnce() -> Output) -> Output where
    Output: Clone + 'static, 
Expand description

Runs init once per topo::CallId. The provided value will always be cloned on subsequent calls unless first dropped from storage before being re-initialized.

Example

use moxie::{once, runtime::RunLoop, testing::CountsClones};
use std::sync::atomic::{AtomicU64, Ordering};

let num_created = AtomicU64::new(0);
let mut rt = RunLoop::new(|| {
    let cached = once(|| {
        num_created.fetch_add(1, Ordering::Relaxed);
        CountsClones::default()
    });
    (num_created.load(Ordering::Relaxed), cached.clone_count())
});

for i in 1..1_000 {
    let (num_created, num_clones) = rt.run_once();
    assert_eq!(num_created, 1, "the first value is always cached");
    assert_eq!(num_clones, i, "cloned once per revision");
}

Environment Expectations

This function requires the following types to be visible to illicit::get and will panic otherwise:

  • Context