Crates.io | once-fn |
lib.rs | once-fn |
version | 0.2.0 |
source | src |
created_at | 2024-10-08 12:06:51.039818 |
updated_at | 2024-10-12 07:47:58.212478 |
description | cache the result of a function, make it runs only once |
homepage | https://github.com/lxl66566/once-fn |
repository | https://github.com/lxl66566/once-fn |
max_upload_size | |
id | 1401058 |
size | 13,528 |
This crate focuses on one simple thing: make a function runs only once. All subsequent calls will return the result of the first call.
Clone
, or a reference points to a type that implements Clone
.impl
clause.use once_fn::once;
#[once]
pub fn foo(b: bool) -> bool {
b
}
assert!(foo(true)); // set the return value to `true`
assert!(foo(false)); // will not run this function twice; directly return `true`.
// allows ref:
#[once]
pub fn foo2(b: &bool) -> &bool {
b
}
for impl block:
use once_fn::once_impl;
struct Foo;
#[once_impl]
impl Foo {
#[once]
pub fn foo(b: bool) -> bool {
b
}
}
see tests for more examples.
cached::proc_macro::once
fn-once
1.61.0 (nightly), 1.70.0 (stable)