| 1 | # fun_time |
| 2 | |
| 3 | [](https://crates.io/crates/fun_time) |
| 4 | [](https://docs.rs/fun_time/0.3.3/fun_time/) |
| 5 | |
| 6 | fun_time is a simple Rust library that allows you to easily time your function calls with a simple attribute! |
| 7 | |
| 8 | ### Basic example |
| 9 | |
| 10 | ```rust |
| 11 | #[fun_time(message = "Heavy calculations on: {a_value}")] |
| 12 | fn some_cool_function(a_value: String) -> usize { |
| 13 | a_value.len() |
| 14 | } |
| 15 | |
| 16 | fn main() { |
| 17 | let my_value_length = some_cool_function(String::from("Hello, world.")); |
| 18 | } |
| 19 | ``` |
| 20 | |
| 21 | The above will print `Starting: Heavy calculations on: Hello, world.` when the function starts, and `Heavy calculations on: Hello, world.: Done in <duration>` on completion. |
| 22 | |
| 23 | ### Configuration |
| 24 | |
| 25 | There are various attributes that allow you to configure the behavior of the `fun_time` attribute. |
| 26 | |
| 27 | - `message` allows you to set a message that will be printed when starting, and when done, the message is passed directly to the `format!` macro, so the arguments to the function can be used in the message (provided they have `Debug` or `Display`). |
| 28 | - `when` allows you to configure when the timing should be collected. The possible values for this are: `"always"` which |
| 29 | as the name might suggest will always collect timing information, and `"debug"` which will only collect when |
| 30 | `cfg!(debug_assertions)` evaluates to `true`. |
| 31 | - `give_back` is a flag that makes it so the wrapped function will now return the elapsed time instead of printing it. |
| 32 | For this it modifies the return type from for example: `-> &'a str` to `-> (&'a str, std::time::Duration)`. This |
| 33 | allows you to handle printing or storing the timing information. |
| 34 | - `reporting` (_can not be used in combination with give_back_) determines how the reporting is done. The possible |
| 35 | options are: `"println"` which will print to stdout using `println!`. The `"log"` option is only available when |
| 36 | the `log` feature is used. This will use the [log](https://crates.io/crates/log) crate with `info!` level logs by default, this can be affected by the `level` option. |
| 37 | - `level` Set the level for the log messages, can by any option that can be parsed by the `log::Level` enum. |
| 38 | |
| 39 | #### Reporting |
| 40 | |
| 41 | The reported messages are formatted as follows: |
| 42 | |
| 43 | **Start message**: "Starting: YOUR_MESSAGE_HERE" |
| 44 | |
| 45 | **Done message**: "YOUR_MESSAGE_HERE: Done in DURATION" |