https://github.com/lxl66566/once-fn
Call function only once, cache the first result
https://github.com/lxl66566/once-fn
macros proc-macro-attributes rust
Last synced: about 1 year ago
JSON representation
Call function only once, cache the first result
- Host: GitHub
- URL: https://github.com/lxl66566/once-fn
- Owner: lxl66566
- License: mit
- Created: 2024-10-07T17:19:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-01T09:21:26.000Z (about 1 year ago)
- Last Synced: 2025-04-13T01:18:36.105Z (about 1 year ago)
- Topics: macros, proc-macro-attributes, rust
- Language: Rust
- Homepage: https://crates.io/crates/once-fn
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
```rust
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:
```rust
use once_fn::once_impl;
struct Foo;
#[once_impl]
impl Foo {
#[once]
pub fn foo(b: bool) -> bool {
b
}
}
```
see [tests](./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
- [x] support impl block