Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions autometrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use proc_macro2::TokenStream;
use quote::quote;
use std::env;
use syn::{parse_macro_input, ImplItem, ItemFn, ItemImpl, Result};
use syn::{parse_macro_input, ImplItem, ItemFn, ItemImpl, Result, ReturnType};

mod parse;

Expand Down Expand Up @@ -50,6 +50,12 @@ fn instrument_function(args: &AutometricsArgs, item: ItemFn) -> Result<TokenStre
// Build the documentation we'll add to the function's RustDocs
let metrics_docs = create_metrics_docs(&prometheus_url, &function_name, args.track_concurrency);

// Type annotation to allow type inference to work on return expressions (such as `.collect()`).
let return_type = match sig.output {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the function returns an impl Trait?

ReturnType::Default => quote! { () },
ReturnType::Type(_, ref t) => quote! { #t },
};

// Wrap the body of the original function, using a slightly different approach based on whether the function is async
let call_function = if sig.asyncness.is_some() {
quote! {
Expand Down Expand Up @@ -136,7 +142,7 @@ fn instrument_function(args: &AutometricsArgs, item: ItemFn) -> Result<TokenStre
AutometricsTracker::start(#gauge_labels)
};

let result = #call_function;
let result: #return_type = #call_function;

{
use autometrics::__private::{HistogramLabels, TrackMetrics};
Expand Down
3 changes: 2 additions & 1 deletion autometrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ mod tracker;
/// Self
/// }
///
/// fn my_function(&self) {
/// fn my_function(&self) -> Vec<usize> {
/// # (0..10).collect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to have this as a test instead of in the example

/// // ...
/// }
/// }
Expand Down