Skip to content

Commit 20ec9cd

Browse files
committed
feat: Able to specify default as sync
1 parent 3e6649f commit 20ec9cd

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ async-trait = "0.1"
4040
features = [ "macros", "rt-multi-thread" ]
4141

4242
[features]
43-
default = [ ]
44-
is_sync = [ ]
43+
is_async = []
44+
is_sync = []
45+
default_sync = []
46+
default = []

src/lib.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ use crate::{parse::Item, visit::AsyncAwaitRemoval};
291291
mod parse;
292292
mod visit;
293293

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+
294303
fn convert_async(input: &mut Item, send: bool) -> TokenStream2 {
295304
if send {
296305
match input {
@@ -346,39 +355,33 @@ fn convert_sync(input: &mut Item) -> TokenStream2 {
346355
#[proc_macro_attribute]
347356
pub fn maybe_async(args: TokenStream, input: TokenStream) -> TokenStream {
348357
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,
356361
};
357362

358363
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+
}
366369
}
367370

368371
/// convert marked async code to async code with `async-trait`
369372
#[proc_macro_attribute]
370373
pub fn must_be_async(args: TokenStream, input: TokenStream) -> TokenStream {
371374
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,
379378
};
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+
}
382385
}
383386

384387
/// convert marked async code to sync code
@@ -395,7 +398,7 @@ pub fn must_be_sync(_args: TokenStream, input: TokenStream) -> TokenStream {
395398
#[proc_macro_attribute]
396399
pub fn sync_impl(_args: TokenStream, input: TokenStream) -> TokenStream {
397400
let input = TokenStream2::from(input);
398-
let token = if cfg!(feature = "is_sync") {
401+
let token = if !is_async!() {
399402
quote!(#input)
400403
} else {
401404
quote!()
@@ -408,24 +411,21 @@ pub fn sync_impl(_args: TokenStream, input: TokenStream) -> TokenStream {
408411
/// only compiled when `is_sync` feature gate is not set.
409412
/// When `is_sync` is set, marked code is removed.
410413
#[proc_macro_attribute]
411-
pub fn async_impl(args: TokenStream, _input: TokenStream) -> TokenStream {
414+
pub fn async_impl(args: TokenStream, input: TokenStream) -> TokenStream {
412415
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,
420419
};
421420

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+
}
429429
}
430430

431431
macro_rules! match_nested_meta_to_str_lit {

0 commit comments

Comments
 (0)