| •1 |
use darlingrust-analyzer cargo darling 0.23.0 crate/::astrust-analyzer cargo darling_core 0.23.0 ast/::NestedMetarust-analyzer cargo darling_core 0.23.0 ast/data/nested_meta/NestedMeta#; |
| 2 |
use darlingrust-analyzer cargo darling 0.23.0 crate/::FromMetarust-analyzer cargo darling_core 0.23.0 from_meta/FromMeta#; |
| 3 |
use quoterust-analyzer cargo quote 1.0.47 crate/::quoterust-analyzer cargo quote 1.0.47 quote!; |
| 4 |
use synrust-analyzer cargo syn 2.0.119 crate/::{parse_macro_inputrust-analyzer cargo syn 2.0.119 parse_macro_input/, ReturnTyperust-analyzer cargo syn 2.0.119 ty/ReturnType#}; |
| 5 |
|
| 6 |
macro_rules! make_darling_errorrust-analyzer cargo fun_time_derive 0.3.4 make_darling_error! { |
| 7 |
($($arg:tt)*) => { |
| 8 |
Err(darling::Error::custom(format!($($arg)*))) |
| 9 |
}; |
| 10 |
} |
| 11 |
|
| 12 |
macro_rules! make_compile_errorrust-analyzer cargo fun_time_derive 0.3.4 make_compile_error! { |
| 13 |
($($arg:tt)*) => { |
| 14 |
quote! { compile_error!($($arg)*) }.into() |
| 15 |
}; |
| 16 |
} |
| 17 |
|
| 18 |
/// Determines when to collect execution time information. |
| 19 |
#[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(FromMetarust-analyzer cargo darling_macro 0.23.0 FromMeta:)] |
| 20 |
enum Whenrust-analyzer cargo fun_time_derive 0.3.4 When# { |
| 21 |
/// Always collect timing information. |
| 22 |
Alwaysrust-analyzer cargo fun_time_derive 0.3.4 When#Always#, |
| 23 |
/// Only collect timing information if `cfg!(debug_assertions)` evaluates to `true`. |
| 24 |
Debugrust-analyzer cargo fun_time_derive 0.3.4 When#Debug#, |
| 25 |
} |
| 26 |
|
| 27 |
/// By default we always collect timing information. |
| 28 |
impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Whenrust-analyzer cargo fun_time_derive 0.3.4 When# { |
| 29 |
fn defaultrust-analyzer cargo fun_time_derive 0.3.4 impl#[When][Default]default().() -> Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[When][Default] { |
| 30 |
Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[When][Default]::Alwaysrust-analyzer cargo fun_time_derive 0.3.4 When#Always# |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl Whenrust-analyzer cargo fun_time_derive 0.3.4 When# { |
| 35 |
/// Parse the [`When`] argument from a given string literal. |
| 36 |
fn from_litrust-analyzer cargo fun_time_derive 0.3.4 impl#[When]from_lit().(literal: synrust-analyzer cargo syn 2.0.119 crate/::LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[When], darlingrust-analyzer cargo darling 0.23.0 crate/::Errorrust-analyzer cargo darling_core 0.23.0 error/Error#> { |
| 37 |
match literal.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() { |
| 38 |
"always" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[When]::Alwaysrust-analyzer cargo fun_time_derive 0.3.4 When#Always#), |
| 39 |
"debug" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[When]::Debugrust-analyzer cargo fun_time_derive 0.3.4 When#Debug#), |
| 40 |
unsupported => make_darling_errorrust-analyzer cargo fun_time_derive 0.3.4 make_darling_error!!( |
| 41 |
"Unsupported value for `when` attribute: {unsupported}. Use one of: always, debug" |
| 42 |
), |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/// Determines how to report the captured execution time information. |
| 48 |
/// |
| 49 |
/// It will print both a start and done message. |
| 50 |
/// The format of the start message is: "Starting: YOUR_MESSAGE_HERE" |
| 51 |
/// The format of the done message is: "YOUR_MESSAGE_HERE: Done in ELAPSED_TIME" |
| 52 |
/// |
| 53 |
/// The `ELAPSED_TIME` is the debug format of [`std::time::Duration`]. |
| 54 |
#[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(FromMetarust-analyzer cargo darling_macro 0.23.0 FromMeta:)] |
| 55 |
enum Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting# { |
| 56 |
/// Use a simple `println!` statement to print the information to the `stdout`. |
| 57 |
Printlnrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Println#, |
| 58 |
/// Use the [log](https://crates.io/crates/log) crate to print the information using the |
| 59 |
/// provided `info!` macro. |
| 60 |
#[cfg(feature = "log")] |
| 61 |
Logrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Log#, |
| 62 |
} |
| 63 |
|
| 64 |
/// By default we use the simple `println!` to write the reporting info to the `stdout`. |
| 65 |
impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting# { |
| 66 |
#[cfg(not(feature = "log"))] |
| 67 |
fn default() -> Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting][Default] { |
| 68 |
Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting][Default]::Println |
| 69 |
} |
| 70 |
|
| 71 |
#[cfg(feature = "log")] |
| 72 |
fn defaultrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting][Default]default().() -> Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting][Default] { |
| 73 |
Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting][Default]::Logrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Log# |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting# { |
| 78 |
/// Parse the [`Reporting`] argument from a given string literal. |
| 79 |
fn from_litrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting]from_lit().(literal: synrust-analyzer cargo syn 2.0.119 crate/::LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting], darlingrust-analyzer cargo darling 0.23.0 crate/::Errorrust-analyzer cargo darling_core 0.23.0 error/Error#> { |
| 80 |
match literal.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() { |
| 81 |
"println" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting]::Printlnrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Println#), |
| 82 |
#[cfg(feature = "log")] |
| 83 |
"log" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting]::Logrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Log#), |
| 84 |
unsupported => make_darling_errorrust-analyzer cargo fun_time_derive 0.3.4 make_darling_error!!("Unsupported value for `reporting` attribute: {unsupported}. Use one of: println, (only with log feature) log") |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg(feature = "log")] |
| 90 |
mod log_levelrust-analyzer cargo fun_time_derive 0.3.4 log_level/ { |
| 91 |
use superrust-analyzer cargo fun_time_derive 0.3.4 crate/::*; |
| 92 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#; |
| 93 |
|
| 94 |
pub struct Levelrust-analyzer cargo fun_time_derive 0.3.4 log_level/Level#(pub logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#); |
| 95 |
|
| 96 |
impl FromMetarust-analyzer cargo darling_core 0.23.0 from_meta/FromMeta# for Levelrust-analyzer cargo fun_time_derive 0.3.4 log_level/Level# {} |
| 97 |
|
| 98 |
impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Levelrust-analyzer cargo fun_time_derive 0.3.4 log_level/Level# { |
| 99 |
fn defaultrust-analyzer cargo fun_time_derive 0.3.4 log_level/impl#[Level][Default]default().() -> Selfrust-analyzer cargo fun_time_derive 0.3.4 log_level/impl#[Level][Default] { |
| 100 |
Selfrust-analyzer cargo fun_time_derive 0.3.4 log_level/impl#[Level][Default](logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#::Inforust-analyzer cargo log 0.4.34 Level#Info#) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl Levelrust-analyzer cargo fun_time_derive 0.3.4 log_level/Level# { |
| 105 |
pub fn from_litrust-analyzer cargo fun_time_derive 0.3.4 log_level/impl#[Level]from_lit().(literal: synrust-analyzer cargo syn 2.0.119 crate/::LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo fun_time_derive 0.3.4 log_level/impl#[Level], darlingrust-analyzer cargo darling 0.23.0 crate/::Errorrust-analyzer cargo darling_core 0.23.0 error/Error#> { |
| 106 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo fun_time_derive 0.3.4 log_level/impl#[Level](logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#::from_strrust-analyzer cargo log 0.4.34 impl#[Level][FromStr]from_str().(&literal.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().()).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| { |
| 107 |
darlingrust-analyzer cargo darling 0.23.0 crate/::Errorrust-analyzer cargo darling_core 0.23.0 error/Error#::customrust-analyzer cargo darling_core 0.23.0 error/impl#[Error]custom().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 108 |
"Unsupported value for `level` attribute: {unsupported}. Use one of: trace, debug, info, warn, error", |
| 109 |
unsupported = literal.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().() |
| 110 |
)) |
| 111 |
})?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().)) |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(FromMetarust-analyzer cargo darling_macro 0.23.0 FromMeta:)] |
| 117 |
struct FunTimeArgsrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs# { |
| 118 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Default]default().)] |
| 119 |
messagerust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#message.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 120 |
/// Determines when we should perform the timing. |
| 121 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(defaultrust-analyzer cargo fun_time_derive 0.3.4 impl#[When][Default]default().)] |
| 122 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then(). = "When::from_lit")] |
| 123 |
whenrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#when.: Whenrust-analyzer cargo fun_time_derive 0.3.4 When#, |
| 124 |
/// Determines whether the elapsed time should be returned or that we log it immediately |
| 125 |
/// to stdout. |
| 126 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/impl#[bool][Default]default().)] |
| 127 |
give_backrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#give_back.: bool, |
| 128 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(defaultrust-analyzer cargo fun_time_derive 0.3.4 impl#[Reporting][Default]default().)] |
| 129 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then(). = "Reporting::from_lit")] |
| 130 |
reportingrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#reporting.: Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting#, |
| 131 |
|
| 132 |
#[cfg(feature = "log")] |
| 133 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(defaultrust-analyzer cargo fun_time_derive 0.3.4 log_level/impl#[Level][Default]default().)] |
| 134 |
#[darlingrust-analyzer cargo darling_macro 0.23.0 darling:(and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then(). = "log_level::Level::from_lit")] |
| 135 |
levelrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#level.: log_levelrust-analyzer cargo fun_time_derive 0.3.4 log_level/::Levelrust-analyzer cargo fun_time_derive 0.3.4 log_level/Level#, |
| 136 |
} |
| 137 |
|
| 138 |
/// Measure the execution times of the function under the attribute. |
| 139 |
/// |
| 140 |
/// It does this by wrapping the function in a new block surrounded by a [`std::time::Instant::now()`] |
| 141 |
/// call and a [`std::time::Instant::elapsed()`] call. It then either logs the duration directly or |
| 142 |
/// returns it with the original functions return value, depending on how you configured this |
| 143 |
/// attribute. |
| 144 |
/// |
| 145 |
/// # Attributes |
| 146 |
/// |
| 147 |
/// ## when |
| 148 |
/// |
| 149 |
/// The `when` attribute can be used to configure when the timing information is collected. For |
| 150 |
/// example, with `"always"` the timing information is always collected, but with `"debug"` the |
| 151 |
/// timing information is only collected if the `cfg!(debug_assertions)` statement evaluates to |
| 152 |
/// `true`. |
| 153 |
/// |
| 154 |
/// ## give_back |
| 155 |
/// |
| 156 |
/// The `give_back` attribute can be used to switch the macro from printing mode to returning the |
| 157 |
/// captured elapsed time together with the original return value of the function. It will modify |
| 158 |
/// the original return value to be a tuple, where the first value is the original return value |
| 159 |
/// and the second value is the elapsed time as a [`std::time::Duration`] struct. |
| 160 |
/// |
| 161 |
/// ## message |
| 162 |
/// |
| 163 |
/// The `message` attribute allows you to set a message that will be displayed in the case you |
| 164 |
/// chose to let the macro report the elapsed time directly. This message will be shown both in |
| 165 |
/// the start and done messages. |
| 166 |
/// |
| 167 |
/// ## reporting |
| 168 |
/// |
| 169 |
/// The `reporting` attribute determines how the message and elapsed time will be displayed |
| 170 |
/// directly when you have chosen not to let the macro return the elapsed time to you. By default |
| 171 |
/// it uses a simple `println!` statement, but with the optional `log` feature it will use the |
| 172 |
/// [log](https://crates.io/crates/log) crate to log it using the `info!` macro. |
| 173 |
/// |
| 174 |
/// # Example |
| 175 |
/// |
| 176 |
/// ``` |
| 177 |
/// // Replace this in your code with `use fun_time::fun_time;` |
| 178 |
/// use fun_time_derive::fun_time; |
| 179 |
/// |
| 180 |
/// #[fun_time(give_back)] |
| 181 |
/// fn function_with_heavy_calculations(some_data: Vec<i32>) -> bool { |
| 182 |
/// // Big brain calculations... |
| 183 |
/// true |
| 184 |
/// } |
| 185 |
/// |
| 186 |
/// fn main() { |
| 187 |
/// let my_data = vec![1, 2, 3]; |
| 188 |
/// |
| 189 |
/// // Run the function and receive timing information |
| 190 |
/// let (my_original_return_value, how_long_did_it_take) = function_with_heavy_calculations(my_data); |
| 191 |
/// } |
| 192 |
/// ``` |
| 193 |
#[proc_macro_attribute] |
| 194 |
pub fn fun_timerust-analyzer cargo fun_time_derive 0.3.4 fun_time:( |
| 195 |
args: proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#, |
| 196 |
item: proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#, |
| 197 |
) -> proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| •198 |
let raw_args = match NestedMetarust-analyzer cargo darling_core 0.23.0 ast/data/nested_meta/NestedMeta#::parse_meta_listrust-analyzer cargo darling_core 0.23.0 ast/data/nested_meta/impl#[NestedMeta]parse_meta_list().(args.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) { |
| 199 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(v) => v, |
| 200 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return darlingrust-analyzer cargo darling 0.23.0 crate/::Errorrust-analyzer cargo darling_core 0.23.0 error/Error#::fromrust-analyzer cargo darling_core 0.23.0 error/impl#[Error][`From<syn::Error>`]from().(e).write_errorsrust-analyzer cargo darling_core 0.23.0 error/impl#[Error]write_errors().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 201 |
}; |
| 202 |
let args: FunTimeArgsrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs# = match FunTimeArgsrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#::from_listrust-analyzer cargo fun_time_derive 0.3.4 impl#[FunTimeArgs][FromMeta]from_list().(&raw_args) { |
| 203 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(args) => args, |
| 204 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => return error.write_errorsrust-analyzer cargo darling_core 0.23.0 error/impl#[Error]write_errors().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 205 |
}; |
| 206 |
|
| 207 |
if args.messagerust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#message..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() && args.give_backrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#give_back. { |
| 208 |
return make_compile_errorrust-analyzer cargo fun_time_derive 0.3.4 make_compile_error!!( |
| 209 |
"the `message` and `give_back` attributes can not be used together!" |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
let item_fn: synrust-analyzer cargo syn 2.0.119 crate/::ItemFnrust-analyzer cargo syn 2.0.119 item/ItemFn# = parse_macro_inputrust-analyzer cargo syn 2.0.119 parse_macro_input/parse_macro_input!!(item as synrust-analyzer cargo syn 2.0.119 crate/::ItemFnrust-analyzer cargo syn 2.0.119 item/ItemFn#); |
| 214 |
|
| 215 |
// Check if we should time the function |
| 216 |
match args.whenrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#when. { |
| 217 |
Whenrust-analyzer cargo fun_time_derive 0.3.4 When#::Debugrust-analyzer cargo fun_time_derive 0.3.4 When#Debug# if args.give_backrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#give_back. => return make_compile_errorrust-analyzer cargo fun_time_derive 0.3.4 make_compile_error!!("the `give_back` and `when` attribute with `\"debug\"` can not be used together! It would result in different return types"), |
| 218 |
Whenrust-analyzer cargo fun_time_derive 0.3.4 When#::Debugrust-analyzer cargo fun_time_derive 0.3.4 When#Debug# if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(debug_assertions) => return quoterust-analyzer cargo quote 1.0.47 quote!! { #item_fn }.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 219 |
_ => {} // No restrictions, go ahead! |
| 220 |
} |
| 221 |
|
| 222 |
let visibility = item_fn.visrust-analyzer cargo syn 2.0.119 item/ItemFn#vis.; |
| 223 |
let signature = item_fn.sigrust-analyzer cargo syn 2.0.119 item/ItemFn#sig..clonerust-analyzer cargo syn 2.0.119 gen/clone/impl#[`crate::Signature`][Clone]clone().(); |
| 224 |
let output = item_fn.sigrust-analyzer cargo syn 2.0.119 item/ItemFn#sig..outputrust-analyzer cargo syn 2.0.119 item/Signature#output.; |
| 225 |
|
| 226 |
// Contains the original logic of the function |
| 227 |
let block = item_fn.blockrust-analyzer cargo syn 2.0.119 item/ItemFn#block.; |
| 228 |
|
| 229 |
// Create wrapped function block |
| 230 |
let wrapped_block = quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 231 |
let super_secret_variable_that_does_not_clash_start = std::time::Instant::now(); |
| 232 |
|
| 233 |
// Immediately invoked closure so a `return` statement in the original function does not |
| 234 |
// break the logging. This also works with self-mutating structs. |
| 235 |
// We also put the original return type as return type for the closure otherwise things like |
| 236 |
// -> Box<dyn Trait> can not be correctly inferred by the compiler. |
| 237 |
let return_value = (|| #output #block)(); |
| 238 |
|
| 239 |
let elapsed = super_secret_variable_that_does_not_clash_start.elapsed(); |
| 240 |
}; |
| 241 |
|
| 242 |
// Create tokens for the `log` call if it is enabled |
| 243 |
#[cfg(feature = "log")] |
| 244 |
let log_tokens = match args.levelrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#level..0rust-analyzer cargo fun_time_derive 0.3.4 log_level/Level#0. { |
| 245 |
logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#::Errorrust-analyzer cargo log 0.4.34 Level#Error# => quoterust-analyzer cargo quote 1.0.47 quote!! { log::error! }, |
| 246 |
logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#::Warnrust-analyzer cargo log 0.4.34 Level#Warn# => quoterust-analyzer cargo quote 1.0.47 quote!! { log::warn! }, |
| 247 |
logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#::Inforust-analyzer cargo log 0.4.34 Level#Info# => quoterust-analyzer cargo quote 1.0.47 quote!! { log::info! }, |
| 248 |
logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#::Debugrust-analyzer cargo log 0.4.34 Level#Debug# => quoterust-analyzer cargo quote 1.0.47 quote!! { log::debug! }, |
| 249 |
logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#::Tracerust-analyzer cargo log 0.4.34 Level#Trace# => quoterust-analyzer cargo quote 1.0.47 quote!! { log::trace! }, |
| 250 |
}; |
| 251 |
|
| 252 |
// Depending on our `give_back` attibute we either return the elapsed time or not |
| 253 |
let tokens = if args.give_backrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#give_back. { |
| 254 |
// Deconstruct the signature because we need to edit the return type |
| 255 |
let synrust-analyzer cargo syn 2.0.119 crate/::Signaturerust-analyzer cargo syn 2.0.119 item/Signature# { |
| 256 |
identrust-analyzer cargo syn 2.0.119 item/Signature#ident., |
| 257 |
genericsrust-analyzer cargo syn 2.0.119 item/Signature#generics., |
| 258 |
inputsrust-analyzer cargo syn 2.0.119 item/Signature#inputs., |
| 259 |
outputrust-analyzer cargo syn 2.0.119 item/Signature#output., |
| 260 |
.. |
| 261 |
} = signature; |
| 262 |
let where_clause = &generics.where_clauserust-analyzer cargo syn 2.0.119 generics/Generics#where_clause.; |
| 263 |
|
| 264 |
// Modify our output type to also return a std::time::Duration (our elapsed time) |
| 265 |
// In case of an empty return type we can simply return the std::time::Duration, otherwise |
| 266 |
// we have to wrap it in a tuple. |
| 267 |
let output_with_duration = match output { |
| 268 |
ReturnTyperust-analyzer cargo syn 2.0.119 ty/ReturnType#::Defaultrust-analyzer cargo syn 2.0.119 ty/ReturnType#Default# => synrust-analyzer cargo syn 2.0.119 crate/::parse_strrust-analyzer cargo syn 2.0.119 parse_str().::<ReturnTyperust-analyzer cargo syn 2.0.119 ty/ReturnType#>("-> std::time::Duration").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 269 |
ReturnTyperust-analyzer cargo syn 2.0.119 ty/ReturnType#::Typerust-analyzer cargo syn 2.0.119 ty/ReturnType#Type#(_, ty) => synrust-analyzer cargo syn 2.0.119 crate/::parse_strrust-analyzer cargo syn 2.0.119 parse_str().::<ReturnTyperust-analyzer cargo syn 2.0.119 ty/ReturnType#>(&formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 270 |
"-> ({}, std::time::Duration)", |
| 271 |
quoterust-analyzer cargo quote 1.0.47 quote!! { #ty } |
| 272 |
)) |
| 273 |
.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 274 |
}; |
| 275 |
|
| 276 |
quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 277 |
#visibility fn #ident #generics (#inputs) #output_with_duration #where_clause { |
| 278 |
#wrapped_block |
| 279 |
|
| 280 |
(return_value, elapsed) |
| 281 |
} |
| 282 |
} |
| 283 |
} else { |
| 284 |
let message = args.messagerust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#message..unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().(); |
| 285 |
|
| 286 |
// Store the message at the top of the function because if the function were to take |
| 287 |
// ownership of the argument it would be gone by the time we want to print the done message. |
| 288 |
let message_statement = quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 289 |
let super_secret_variable_that_does_not_clash_message = format!(#message); |
| 290 |
}; |
| 291 |
|
| 292 |
let starting_statement = match args.reportingrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#reporting. { |
| 293 |
Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting#::Printlnrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Println# => quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 294 |
println!("{}", super_secret_variable_that_does_not_clash_message); |
| 295 |
}, |
| 296 |
#[cfg(feature = "log")] |
| 297 |
Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting#::Logrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Log# => quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 298 |
#log_tokens("{}", super_secret_variable_that_does_not_clash_message); |
| 299 |
}, |
| 300 |
}; |
| 301 |
|
| 302 |
let reporting_statement = match args.reportingrust-analyzer cargo fun_time_derive 0.3.4 FunTimeArgs#reporting. { |
| 303 |
Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting#::Printlnrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Println# => quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 304 |
println!("{}: Done in {:.2?}", super_secret_variable_that_does_not_clash_message, elapsed); |
| 305 |
}, |
| 306 |
#[cfg(feature = "log")] |
| 307 |
Reportingrust-analyzer cargo fun_time_derive 0.3.4 Reporting#::Logrust-analyzer cargo fun_time_derive 0.3.4 Reporting#Log# => quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 308 |
#log_tokens("{}: Done in {:.2?}", super_secret_variable_that_does_not_clash_message, elapsed); |
| 309 |
}, |
| 310 |
}; |
| 311 |
|
| 312 |
quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 313 |
#visibility #signature { |
| 314 |
#message_statement |
| 315 |
#starting_statement |
| 316 |
|
| 317 |
#wrapped_block |
| 318 |
|
| 319 |
#reporting_statement |
| 320 |
|
| 321 |
return_value |
| 322 |
} |
| 323 |
} |
| 324 |
}; |
| 325 |
|
| 326 |
tokens.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 327 |
} |