@@ -291,6 +291,15 @@ use crate::{parse::Item, visit::AsyncAwaitRemoval};
291
291
mod parse;
292
292
mod visit;
293
293
294
+ #[ cfg( all( feature = "is_sync" , feature = "is_async" ) ) ]
295
+ compile_error ! ( "feature \" is_sync\" and feature \" is_async\" cannot be enabled at the same time" ) ;
296
+
297
+ macro_rules! is_async {
298
+ ( ) => {
299
+ cfg!( feature = "is_async" ) || !( cfg!( feature = "is_sync" ) || cfg!( feature = "default_sync" ) )
300
+ } ;
301
+ }
302
+
294
303
fn convert_async ( input : & mut Item , send : bool ) -> TokenStream2 {
295
304
if send {
296
305
match input {
@@ -346,23 +355,22 @@ fn convert_sync(input: &mut Item) -> TokenStream2 {
346
355
#[ proc_macro_attribute]
347
356
pub fn maybe_async ( args : TokenStream , input : TokenStream ) -> TokenStream {
348
357
let send = match args. to_string ( ) . replace ( " " , "" ) . as_str ( ) {
349
- "" | "Send" => true ,
350
- "?Send" => false ,
351
- _ => {
352
- return syn:: Error :: new ( Span :: call_site ( ) , "Only accepts `Send` or `?Send`" )
353
- . to_compile_error ( )
354
- . into ( ) ;
355
- }
358
+ "" | "Send" => Some ( true ) ,
359
+ "?Send" => Some ( false ) ,
360
+ _ => None ,
356
361
} ;
357
362
358
- let mut item = parse_macro_input ! ( input as Item ) ;
359
-
360
- let token = if cfg ! ( feature = "is_sync" ) {
361
- convert_sync ( & mut item)
362
- } else {
363
- convert_async ( & mut item, send)
364
- } ;
365
- token. into ( )
363
+ match ( is_async ! ( ) , send) {
364
+ ( true , Some ( send) ) => {
365
+ let mut item = parse_macro_input ! ( input as Item ) ;
366
+ convert_async ( & mut item, send) . into ( )
367
+ }
368
+ ( false , _) => {
369
+ let mut item = parse_macro_input ! ( input as Item ) ;
370
+ convert_sync ( & mut item) . into ( )
371
+ }
372
+ _ => input,
373
+ }
366
374
}
367
375
368
376
/// convert marked async code to async code with `async-trait`
@@ -395,7 +403,7 @@ pub fn must_be_sync(_args: TokenStream, input: TokenStream) -> TokenStream {
395
403
#[ proc_macro_attribute]
396
404
pub fn sync_impl ( _args : TokenStream , input : TokenStream ) -> TokenStream {
397
405
let input = TokenStream2 :: from ( input) ;
398
- let token = if cfg ! ( feature = "is_sync" ) {
406
+ let token = if ! is_async ! ( ) {
399
407
quote ! ( #input)
400
408
} else {
401
409
quote ! ( )
@@ -419,13 +427,12 @@ pub fn async_impl(args: TokenStream, _input: TokenStream) -> TokenStream {
419
427
}
420
428
} ;
421
429
422
- let token = if cfg ! ( feature = "is_sync" ) {
423
- quote ! ( )
424
- } else {
430
+ if is_async ! ( ) {
425
431
let mut item = parse_macro_input ! ( _input as Item ) ;
426
- convert_async ( & mut item, send)
427
- } ;
428
- token. into ( )
432
+ convert_async ( & mut item, send) . into ( )
433
+ } else {
434
+ quote ! ( ) . into ( )
435
+ }
429
436
}
430
437
431
438
macro_rules! match_nested_meta_to_str_lit {
0 commit comments