-
Notifications
You must be signed in to change notification settings - Fork 276
/
lib.rs
66 lines (59 loc) · 1.61 KB
/
lib.rs
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
57
58
59
60
61
62
63
64
65
66
//! Crate with derive macros for futures
#![allow(clippy::expect_used, clippy::str_to_string)]
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::{abort, proc_macro_error};
use quote::quote;
use syn::{parse_macro_input, Generics, ItemFn, ReturnType, Signature};
fn impl_telemetry_future(
ItemFn {
attrs,
vis,
sig,
block,
}: ItemFn,
) -> TokenStream2 {
let Signature {
asyncness,
ident,
generics: Generics {
params,
where_clause,
..
},
inputs,
output,
..
} = sig;
if asyncness.is_none() {
abort!(
asyncness,
"Function should be async for using telemetry_future"
);
}
let output = match &output {
ReturnType::Type(_, tp) => quote! { #tp },
ReturnType::Default => quote! { () },
};
quote! {
#(#attrs)*
#vis async fn #ident < #params > ( #inputs ) -> #output
#where_clause
{
let __future_name = concat!(module_path!(), "::", stringify!(#ident));
iroha_futures::TelemetryFuture::new(async #block, __future_name).await
}
}
}
/// Macro for wrapping future for getting telemetry info about poll times and numbers
#[proc_macro_error]
#[proc_macro_attribute]
pub fn telemetry_future(_args: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemFn);
if cfg!(feature = "telemetry") {
impl_telemetry_future(input)
} else {
quote! { #input }
}
.into()
}