once-fn
This crate focuses on one simple thing: make a function runs only once. All subsequent calls will return the result of the first call.
Limitations
- Return type must satisfy:
- Implements
Clone, or a reference points to a type that implementsClone. - could not be generics type or
implclause.
- Implements
Example
use once;
assert!; // set the return value to `true`
assert!; // will not run this function twice; directly return `true`.
// allows ref:
for impl block:
use once_impl;
;
see tests for more examples.
Why not
cached::proc_macro::once- does not support async fn
- does not support generics (in input)
- does not support reference (in return type)
- does not support use in impl block
fn-once- Almost no docs; I don't know what it actually do.
- It can't even compile its example
MSRV
1.61.0 (nightly), 1.70.0 (stable)
todo
- support impl block