@@ -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,39 +355,33 @@ 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
363
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 ( )
364
+ match ( is_async ! ( ) , send) {
365
+ ( true , Some ( send) ) => convert_async ( & mut item, send) . into ( ) ,
366
+ ( false , _) => convert_sync ( & mut item) . into ( ) ,
367
+ _ => quote ! ( #item) . into ( ) ,
368
+ }
366
369
}
367
370
368
371
/// convert marked async code to async code with `async-trait`
369
372
#[ proc_macro_attribute]
370
373
pub fn must_be_async ( args : TokenStream , input : TokenStream ) -> TokenStream {
371
374
let send = match args. to_string ( ) . replace ( " " , "" ) . as_str ( ) {
372
- "" | "Send" => true ,
373
- "?Send" => false ,
374
- _ => {
375
- return syn:: Error :: new ( Span :: call_site ( ) , "Only accepts `Send` or `?Send`" )
376
- . to_compile_error ( )
377
- . into ( ) ;
378
- }
375
+ "" | "Send" => Some ( true ) ,
376
+ "?Send" => Some ( false ) ,
377
+ _ => None ,
379
378
} ;
380
- let mut item = parse_macro_input ! ( input as Item ) ;
381
- convert_async ( & mut item, send) . into ( )
379
+ if let Some ( send) = send {
380
+ let mut item = parse_macro_input ! ( input as Item ) ;
381
+ convert_async ( & mut item, send) . into ( )
382
+ } else {
383
+ input
384
+ }
382
385
}
383
386
384
387
/// convert marked async code to sync code
@@ -395,7 +398,7 @@ pub fn must_be_sync(_args: TokenStream, input: TokenStream) -> TokenStream {
395
398
#[ proc_macro_attribute]
396
399
pub fn sync_impl ( _args : TokenStream , input : TokenStream ) -> TokenStream {
397
400
let input = TokenStream2 :: from ( input) ;
398
- let token = if cfg ! ( feature = "is_sync" ) {
401
+ let token = if ! is_async ! ( ) {
399
402
quote ! ( #input)
400
403
} else {
401
404
quote ! ( )
@@ -408,24 +411,21 @@ pub fn sync_impl(_args: TokenStream, input: TokenStream) -> TokenStream {
408
411
/// only compiled when `is_sync` feature gate is not set.
409
412
/// When `is_sync` is set, marked code is removed.
410
413
#[ proc_macro_attribute]
411
- pub fn async_impl ( args : TokenStream , _input : TokenStream ) -> TokenStream {
414
+ pub fn async_impl ( args : TokenStream , input : TokenStream ) -> TokenStream {
412
415
let send = match args. to_string ( ) . replace ( " " , "" ) . as_str ( ) {
413
- "" | "Send" => true ,
414
- "?Send" => false ,
415
- _ => {
416
- return syn:: Error :: new ( Span :: call_site ( ) , "Only accepts `Send` or `?Send`" )
417
- . to_compile_error ( )
418
- . into ( ) ;
419
- }
416
+ "" | "Send" => Some ( true ) ,
417
+ "?Send" => Some ( false ) ,
418
+ _ => None ,
420
419
} ;
421
420
422
- let token = if cfg ! ( feature = "is_sync" ) {
423
- quote ! ( )
424
- } else {
425
- let mut item = parse_macro_input ! ( _input as Item ) ;
426
- convert_async ( & mut item, send)
427
- } ;
428
- token. into ( )
421
+ match ( is_async ! ( ) , send) {
422
+ ( true , Some ( send) ) => {
423
+ let mut item = parse_macro_input ! ( input as Item ) ;
424
+ convert_async ( & mut item, send) . into ( )
425
+ }
426
+ ( false , _) => quote ! ( ) . into ( ) ,
427
+ _ => input,
428
+ }
429
429
}
430
430
431
431
macro_rules! match_nested_meta_to_str_lit {
0 commit comments