1+ mod  args; 
2+ 
3+ use  args:: AttrOptions ; 
14use  proc_macro:: TokenStream ; 
25use  proc_macro_crate:: { crate_name,  FoundCrate } ; 
3- use  quote:: { format_ident,  quote} ; 
4- use  syn:: { 
5-     parse:: Parse , 
6-     parse_macro_input, 
7-     punctuated:: Punctuated , 
8-     ItemFn , 
9-     Meta :: { self ,  NameValue } , 
10-     MetaNameValue ,  Token , 
11- } ; 
12- 
13- struct  MyBenchArgs  { 
14-     args :  Punctuated < Meta ,  Token ! [ , ] > , 
15- } 
16- 
17- impl  Parse  for  MyBenchArgs  { 
18-     fn  parse ( input :  syn:: parse:: ParseStream )  -> syn:: Result < Self >  { 
19-         Ok ( Self  { 
20-             args :  Punctuated :: parse_terminated ( input) ?, 
21-         } ) 
22-     } 
23- } 
6+ use  quote:: { format_ident,  quote,  ToTokens } ; 
7+ use  syn:: { parse_macro_input,  Expr ,  ItemFn ,  Meta } ; 
248
259#[ proc_macro_attribute]  
2610pub  fn  bench_compat ( attr :  TokenStream ,  item :  TokenStream )  -> TokenStream  { 
27-     let  parsed_args = parse_macro_input ! ( attr as  MyBenchArgs ) ; 
2811    let  input = parse_macro_input ! ( item as  ItemFn ) ; 
2912
30-     let  mut  filtered_args = Vec :: new ( ) ; 
31- 
32-     for  arg in  parsed_args. args  { 
33-         match  & arg { 
34-             NameValue ( MetaNameValue  {  path,  .. } )  => { 
35-                 if  path. is_ident ( "crate" )  { 
36-                     return  quote !  { 
37-                         compile_error!( "`crate` argument is not supported with codspeed_divan_compat" ) ; 
38-                     } . 
39-                     into ( ) ; 
40-                 } 
41- 
42-                 if  path. is_ident ( "types" )  { 
43-                     return  quote !  { 
44-                         compile_error!( "`type` argument is not yet supported with codspeed_divan_compat" ) ; 
45-                     } 
46-                     . into ( ) ; 
47-                 } 
48- 
49-                 if  path. is_ident ( "min_time" ) 
50-                     || path. is_ident ( "max_time" ) 
51-                     || path. is_ident ( "sample_size" ) 
52-                     || path. is_ident ( "sample_count" ) 
53-                     || path. is_ident ( "skip_ext_time" ) 
54-                 { 
55-                     // These arguments are ignored in instrumented mode 
56-                     continue ; 
57-                 } 
13+     let  attr_options = match  AttrOptions :: parse ( attr)  { 
14+         Ok ( attr_options)  => attr_options, 
15+         Err ( error)  => return  error, 
16+     } ; 
5817
59-                 filtered_args . push ( arg ) ; 
60-              } 
61-             _ => filtered_args . push ( arg ) , 
18+     if  attr_options . crate_   { 
19+         return   quote !   { 
20+             compile_error! ( "`crate` argument is yet supported with codspeed_divan_compat" ) ; 
6221        } 
22+         . into ( ) ; 
6323    } 
6424
6525    let  codspeed_divan_crate_ident = format_ident ! ( 
@@ -72,10 +32,21 @@ pub fn bench_compat(attr: TokenStream, item: TokenStream) -> TokenStream {
7232            . unwrap_or( "codspeed_divan_compat" . to_string( ) ) 
7333    ) ; 
7434
75-     filtered_args. push ( syn:: parse_quote!( crate  = :: #codspeed_divan_crate_ident) ) ; 
76-     // Important: keep macro name in sync with re-exported macro name in divan-compat lib 
35+     let  mut  transfered_args = attr_options. other_args ; 
36+ 
37+     transfered_args. push ( syn:: parse_quote!( crate  = :: #codspeed_divan_crate_ident) ) ; 
38+ 
39+     if  let  Some ( types)  = attr_options. types  { 
40+         transfered_args. push ( Meta :: NameValue ( syn:: MetaNameValue  { 
41+             path :  syn:: parse_quote!( types) , 
42+             eq_token :  Default :: default ( ) , 
43+             value :  Expr :: Verbatim ( types. into_token_stream ( ) ) , 
44+         } ) ) ; 
45+     } 
46+ 
47+     // WARN: keep macro name in sync with re-exported macro name in divan-compat lib 
7748    let  expanded = quote !  { 
78-         #[ :: #codspeed_divan_crate_ident:: bench_original( #( #filtered_args ) , * ) ] 
49+         #[ :: #codspeed_divan_crate_ident:: bench_original( #( #transfered_args ) , * ) ] 
7950        #input
8051    } ; 
8152
0 commit comments