fn_once

Crates.iofn_once
lib.rsfn_once
version0.3.0
sourcesrc
created_at2022-09-25 14:56:47.818064
updated_at2022-09-28 07:50:06.889353
descriptionThis library provides a convenient derive macro for a once function.
homepagehttps://github.com/just-do-halee/fn_once
repositoryhttps://github.com/just-do-halee/fn_once
max_upload_size
id673656
size18,424
Doha Lee (just-do-halee)

documentation

README

fn_once

This library provides a convenient derive macro for a once function.

CI Crates.io Licensed Twitter

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

cargo fmt