performance_mark_macro/
lib.rs

1//! This crate implements the macro for `performance_mark` and should not be used directly.
2extern crate proc_macro;
3
4use proc_macro::TokenStream;
5use syn::parse_macro_input;
6
7#[proc_macro_attribute]
8/// performance_mark is an attribute macro that adds performance (time) logging to methods.
9/// By default, it uses `println!`, but can be configured to use a custom method.
10///
11/// Basic example:
12/// ```no_run no_test
13/// use performance_mark_attribute::performance_mark;
14///
15/// #[performance_mark]
16/// fn test_function_logged_using_stdout() {
17///    println!("Hello!");
18/// }
19/// ```
20///
21/// Example with a custom logging method:
22/// ```no_run no_test
23/// use performance_mark_attribute::{performance_mark, LogContext}
24///
25/// #[performance_mark(log_with_this)]
26/// fn test_function_logged_using_custom_function() {
27///    println!("Hello!");
28/// }
29///
30/// fn log_with_this(ctx: LogContext) {
31///     println!("Function: {} , Time: {}ms", ctx.function, ctx.duration);
32/// }
33/// ```
34///
35/// Example with a custom async logging method:
36/// ```no_run no_test
37/// use performance_mark_attribute::{performance_mark, LogContext}
38///
39/// #[performance_mark(async log_with_this)]
40/// fn test_function_logged_using_custom_function() {
41///    println!("Hello!");
42/// }
43///
44/// async fn log_with_this(ctx: LogContext) {
45///     // Log asynchronously
46/// }
47/// ```
48pub fn performance_mark(attr: TokenStream, item: TokenStream) -> TokenStream {
49    let attr = parse_macro_input!(attr as proc_macro2::TokenStream);
50    let item = parse_macro_input!(item as proc_macro2::TokenStream);
51
52    match performance_mark_impl::performance_mark(attr, item) {
53        Ok(tokens) => tokens.into(),
54        Err(err) => TokenStream::from(err.to_compile_error()),
55    }
56}