fun_time-0.3.4/.cargo_vcs_info.json0000644000000001360000000000100126460ustar { "git": { "sha1": "ec07669d8a9e5effb27ab48f38dece62be0643ba" }, "path_in_vcs": "" }fun_time-0.3.4/.gitignore000064400000000000000000000000141046102023000134210ustar 00000000000000target .ideafun_time-0.3.4/Cargo.toml0000644000000022400000000000100106420ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "fun_time" version = "0.3.4" authors = ["Steven Liebregt "] description = "fun_time is a simple Rust library that allows you to easily time your function calls with a simple attribute!" readme = "README.md" keywords = [ "function", "timing", "execution_time", "measure", "time", ] categories = ["development-tools::profiling"] license = "MIT" repository = "https://github.com/stevenliebregt/fun_time" [dependencies.fun_time_derive] version = "0.3.4" [dependencies.log] version = "0.4.17" optional = true [dev-dependencies.simple_logger] version = "4.2.0" [features] default = [] log = [ "dep:log", "fun_time_derive/log", ] fun_time-0.3.4/Cargo.toml.orig000064400000000000000000000012441046102023000143260ustar 00000000000000[package] name = "fun_time" description = "fun_time is a simple Rust library that allows you to easily time your function calls with a simple attribute!" authors = ["Steven Liebregt "] version = "0.3.4" edition = "2021" license = "MIT" repository = "https://github.com/stevenliebregt/fun_time" keywords = ["function", "timing", "execution_time", "measure", "time"] categories = ["development-tools::profiling"] [dependencies] fun_time_derive = { version = "0.3.4", path = "fun_time_derive" } log = { version = "0.4.17", optional = true } [features] default = [] log = ["dep:log", "fun_time_derive/log"] [dev-dependencies] simple_logger = "4.2.0"fun_time-0.3.4/LICENSE000064400000000000000000000020601046102023000124410ustar 00000000000000MIT License Copyright (c) 2022 Steven Liebregt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. fun_time-0.3.4/README.md000064400000000000000000000044761046102023000127300ustar 00000000000000# fun_time [![Crates.io](https://img.shields.io/crates/v/fun_time)](https://crates.io/crates/fun_time) [![docs.rs](https://img.shields.io/docsrs/fun_time)](https://docs.rs/fun_time/0.3.3/fun_time/) fun_time is a simple Rust library that allows you to easily time your function calls with a simple attribute! ### Basic example ```rust #[fun_time(message = "Heavy calculations on: {a_value}")] fn some_cool_function(a_value: String) -> usize { a_value.len() } fn main() { let my_value_length = some_cool_function(String::from("Hello, world.")); } ``` The above will print `Starting: Heavy calculations on: Hello, world.` when the function starts, and `Heavy calculations on: Hello, world.: Done in ` on completion. ### Configuration There are various attributes that allow you to configure the behavior of the `fun_time` attribute. - `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`). - `when` allows you to configure when the timing should be collected. The possible values for this are: `"always"` which as the name might suggest will always collect timing information, and `"debug"` which will only collect when `cfg!(debug_assertions)` evaluates to `true`. - `give_back` is a flag that makes it so the wrapped function will now return the elapsed time instead of printing it. For this it modifies the return type from for example: `-> &'a str` to `-> (&'a str, std::time::Duration)`. This allows you to handle printing or storing the timing information. - `reporting` (_can not be used in combination with give_back_) determines how the reporting is done. The possible options are: `"println"` which will print to stdout using `println!`. The `"log"` option is only available when 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. - `level` Set the level for the log messages, can by any option that can be parsed by the `log::Level` enum. #### Reporting The reported messages are formatted as follows: **Start message**: "Starting: YOUR_MESSAGE_HERE" **Done message**: "YOUR_MESSAGE_HERE: Done in DURATION" fun_time-0.3.4/src/lib.rs000064400000000000000000000073441046102023000133510ustar 00000000000000pub use fun_time_derive::*; #[cfg(test)] mod tests { use fun_time_derive::fun_time; use std::fmt::Debug; use std::time::Duration; #[fun_time(give_back)] fn dummy_test_function_that_sleeps<'a, T>(borrowed_thing: &'a T) -> &'a T where T: Debug + Sized, { std::thread::sleep(Duration::from_millis(42)); println!("the borrowed_thing is = {borrowed_thing:?}"); borrowed_thing } #[test] fn works_with_struct_member_that_modifies() { struct Thing { value: i32, } impl Thing { pub fn new(value: i32) -> Self { Self { value } } #[fun_time(message = "modify while mutating self", reporting = "println")] pub fn modify_timed(&mut self, new_value: i32) -> i32 { let old_value = self.value; self.value = new_value; old_value } } let mut thing = Thing::new(1); let _old_value = thing.modify_timed(1337); } #[test] fn works_with_traits() { trait MyTrait { fn speak(&self) -> &'static str; } struct A {} impl MyTrait for A { fn speak(&self) -> &'static str { "I am: A" } } struct B {} impl MyTrait for B { fn speak(&self) -> &'static str { "I am: B" } } #[derive(Debug)] enum MyEnum { A, B, } impl MyEnum { #[fun_time(message = "Getting trait item for: {self:?}")] fn get_trait_item(&self) -> Box { match self { Self::A => Box::new(A {}), Self::B => Box::new(B {}), } } } let enum_a = MyEnum::A; let _ = enum_a.get_trait_item().speak(); let enum_b = MyEnum::B; let _ = enum_b.get_trait_item().speak(); } #[cfg(feature = "log")] mod feature_log_tests { use super::*; use simple_logger::SimpleLogger; use std::fmt::Formatter; #[fun_time(when = "debug", message = "having fun with log", reporting = "log")] fn have_fun(_first: String, _second: String) {} struct Parameter { name: String, value: i32, } impl Debug for Parameter { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { if f.alternate() { write!(f, "name = {} and value * 2 = {}", self.name, self.value * 2) } else { write!(f, "name = {} and value = {}", self.name, self.value) } } } #[fun_time( message = "having fun with parameters: {first:#?} and {second}", level = "debug", reporting = "log" )] fn have_fun_with_parameters(first: Parameter, second: i32) { // Let's take ownership of the first parameter let _first = first; } #[test] fn it_works() { let (borrowed_thing, _elapsed_time) = dummy_test_function_that_sleeps(&"Hello, there!"); assert_eq!(&"Hello, there!", borrowed_thing); SimpleLogger::new().init().unwrap_or(()); have_fun("Alice".to_string(), "Bob".to_string()); } #[test] fn it_works_with_parameters() { SimpleLogger::new().init().unwrap_or(()); have_fun_with_parameters( Parameter { name: "Alice".to_string(), value: 1234, }, 1234, ); } } }