[−][src]Function moxie::once_with
pub fn once_with<Output, Ret>(
init: impl FnOnce() -> Output,
with: impl FnOnce(&Output) -> Ret
) -> Ret where
Output: 'static,
Ret: 'static,
Caches init
once in the current topo::CallId
. Runs with
on every
runtime::Revision
.
Example
use moxie::{once_with, runtime::RunLoop, testing::CountsClones}; use std::sync::atomic::{AtomicU64, Ordering}; let num_created = AtomicU64::new(0); let mut rt = RunLoop::new(|| { let cached = once_with( || { num_created.fetch_add(1, Ordering::Relaxed); CountsClones::default() }, // this makes it equivalent to calling moxie::once(...) CountsClones::clone, ); (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