2121use  proc_macro:: TokenStream ; 
2222use  quote:: quote; 
2323use  syn:: spanned:: Spanned ; 
24- use  syn:: { parse,  ImplItemFn ,  ItemImpl ,  ItemTrait ,  Token } ; 
25- 
26- fn  add_async_trait ( mut  parsed :  ItemTrait )  -> TokenStream  { 
27- 	let  output = quote !  { 
28- 		#[ cfg( not( feature = "async-interface" ) ) ] 
29- 		#parsed
30- 	} ; 
31- 
32- 	for  mut  item in  & mut  parsed. items  { 
33- 		if  let  syn:: TraitItem :: Fn ( f)  = & mut  item { 
34- 			f. sig . asyncness  = Some ( Token ! [ async ] ( f. span ( ) ) ) ; 
35- 		} 
36- 	} 
37- 
38- 	let  output = quote !  { 
39- 		#output
40- 
41- 		#[ cfg( feature = "async-interface" ) ] 
42- 		#[ async_trait( ?Send ) ] 
43- 		#parsed
44- 	} ; 
45- 
46- 	output. into ( ) 
47- } 
24+ use  syn:: { parse,  ImplItemFn ,  Token } ; 
4825
4926fn  add_async_method ( mut  parsed :  ImplItemFn )  -> TokenStream  { 
5027	let  output = quote !  { 
@@ -64,44 +41,14 @@ fn add_async_method(mut parsed: ImplItemFn) -> TokenStream {
6441	output. into ( ) 
6542} 
6643
67- fn  add_async_impl_trait ( mut  parsed :  ItemImpl )  -> TokenStream  { 
68- 	let  output = quote !  { 
69- 		#[ cfg( not( feature = "async-interface" ) ) ] 
70- 		#parsed
71- 	} ; 
72- 
73- 	for  mut  item in  & mut  parsed. items  { 
74- 		if  let  syn:: ImplItem :: Fn ( f)  = & mut  item { 
75- 			f. sig . asyncness  = Some ( Token ! [ async ] ( f. span ( ) ) ) ; 
76- 		} 
77- 	} 
78- 
79- 	let  output = quote !  { 
80- 		#output
81- 
82- 		#[ cfg( feature = "async-interface" ) ] 
83- 		#[ async_trait( ?Send ) ] 
84- 		#parsed
85- 	} ; 
86- 
87- 	output. into ( ) 
88- } 
89- 
90- /// Makes a method or every method of a trait `async`, if the `async-interface` feature is enabled. 
91- /// 
92- /// Requires the `async-trait` crate as a dependency whenever this attribute is used on a trait 
93- /// definition or trait implementation. 
44+ /// Makes a method `async`, if the `async-interface` feature is enabled. 
9445#[ proc_macro_attribute]  
9546pub  fn  maybe_async ( _attr :  TokenStream ,  item :  TokenStream )  -> TokenStream  { 
96- 	if  let  Ok ( parsed)  = parse ( item. clone ( ) )  { 
97- 		add_async_trait ( parsed) 
98- 	}  else  if  let  Ok ( parsed)  = parse ( item. clone ( ) )  { 
47+ 	if  let  Ok ( parsed)  = parse ( item)  { 
9948		add_async_method ( parsed) 
100- 	}  else  if  let  Ok ( parsed)  = parse ( item)  { 
101- 		add_async_impl_trait ( parsed) 
10249	}  else  { 
10350		( quote !  { 
104- 			compile_error!( "#[maybe_async] can only be used on methods, trait or trait impl blocks " ) 
51+ 			compile_error!( "#[maybe_async] can only be used on methods" ) 
10552		} ) 
10653		. into ( ) 
10754	} 
0 commit comments