once-fn

Crates.ioonce-fn
lib.rsonce-fn
version0.2.0
sourcesrc
created_at2024-10-08 12:06:51.039818
updated_at2024-10-12 07:47:58.212478
descriptioncache the result of a function, make it runs only once
homepagehttps://github.com/lxl66566/once-fn
repositoryhttps://github.com/lxl66566/once-fn
max_upload_size
id1401058
size13,528
Absolutex (lxl66566)

documentation

README

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 implements Clone.
    • could not be generics type or impl clause.

Example

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.

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
Commit count: 6

cargo fmt