1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![deny(clippy::cargo)]
#![allow(unreachable_code)]

use proc_macro::TokenStream;
use quote::quote;
use syn::ItemFn;

/// Time the duration of a function, either to stdout or via `tracing`.
#[proc_macro_attribute]
pub fn time_function(_args: TokenStream, input: TokenStream) -> TokenStream {
    // Do nothing if release and not using the release feature
    #[cfg(all(not(debug_assertions), not(feature = "release")))]
    return input;

    // Parse the input token stream as a function
    let input = syn::parse_macro_input!(input as ItemFn);

    // Extract the function's signature and body
    let func_name = &input.sig.ident;
    let func_block = &input.block;
    let func_output = &input.sig.output;

    // Generate the wrapped function
    let output = if input.sig.asyncness.is_some() {
        quote! {
            async fn #func_name() #func_output {
                let start = std::time::Instant::now();
                let result = (|| async #func_block)().await;
                let duration: std::time::Duration = start.elapsed();
                #[cfg(not(feature = "tracing"))]
                println!("Function `{}` took {:?}", stringify!(#func_name), duration);
                #[cfg(feature = "tracing")]
                tracing::trace!("Function `{}` took {:?}", stringify!(#func_name), duration);
                result
            }
        }
    } else {
        quote! {
            fn #func_name() #func_output {
                let start = std::time::Instant::now();
                let result = (|| #func_block)();
                let duration: std::time::Duration = start.elapsed();
                #[cfg(not(feature = "tracing"))]
                println!("Function `{}` took {:?}", stringify!(#func_name), duration);
                #[cfg(feature = "tracing")]
                tracing::trace!("Function `{}` took {:?}", stringify!(#func_name), duration);
                result
            }
        }
    };

    // Return the generated code as a token stream
    output.into()
}