21
21
use proc_macro:: TokenStream ;
22
22
use quote:: quote;
23
23
use 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 } ;
48
25
49
26
fn add_async_method ( mut parsed : ImplItemFn ) -> TokenStream {
50
27
let output = quote ! {
@@ -64,44 +41,14 @@ fn add_async_method(mut parsed: ImplItemFn) -> TokenStream {
64
41
output. into ( )
65
42
}
66
43
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.
94
45
#[ proc_macro_attribute]
95
46
pub 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) {
99
48
add_async_method ( parsed)
100
- } else if let Ok ( parsed) = parse ( item) {
101
- add_async_impl_trait ( parsed)
102
49
} else {
103
50
( 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" )
105
52
} )
106
53
. into ( )
107
54
}
0 commit comments