Crates.io | fn_once |
lib.rs | fn_once |
version | 0.3.0 |
source | src |
created_at | 2022-09-25 14:56:47.818064 |
updated_at | 2022-09-28 07:50:06.889353 |
description | This library provides a convenient derive macro for a once function. |
homepage | https://github.com/just-do-halee/fn_once |
repository | https://github.com/just-do-halee/fn_once |
max_upload_size | |
id | 673656 |
size | 18,424 |
fn_once
This library provides a convenient derive macro for a once function.
fn_once = "0.3.0"
How to use,
use fn_once::once;
#[once]
fn print_once(name: &str) {
println!("Hello! {}!", name);
}
#[once(or = { number })] // or = { block expression }
fn add_one_once(number: u32) -> u32 {
count + 1
}
#[once(panic)]
fn panic_when_twice() { }
fn main() {
print_once("World"); // print "Hello! World!"
print_once("foo"); // no effect
let mut number = 0;
number = add_one_once(number); // 0 + 1 -> 1
number = add_one_once(number); // no effect -> 1
number = add_one_once(number); // no effect -> 1
assert_eq!(number, 1);
panic_when_twice();
panic_when_twice(); // panic!
}
When you want to disable it,
[features]
default = ["once_off"]
once_off = []
# The point is that
# just `once_off` feature is turned on.