Crates.io | once-fn |
lib.rs | once-fn |
version | 0.2.1 |
created_at | 2024-10-08 12:06:51.039818+00 |
updated_at | 2025-08-18 11:47:30.883572+00 |
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 | 23,110 |
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)