Skip to content

Commit 6480a01

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

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

Cargo.toml

+4-2
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

+29-22
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,23 +355,22 @@ 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

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+
}
366374
}
367375

368376
/// convert marked async code to async code with `async-trait`
@@ -395,7 +403,7 @@ pub fn must_be_sync(_args: TokenStream, input: TokenStream) -> TokenStream {
395403
#[proc_macro_attribute]
396404
pub fn sync_impl(_args: TokenStream, input: TokenStream) -> TokenStream {
397405
let input = TokenStream2::from(input);
398-
let token = if cfg!(feature = "is_sync") {
406+
let token = if !is_async!() {
399407
quote!(#input)
400408
} else {
401409
quote!()
@@ -419,13 +427,12 @@ pub fn async_impl(args: TokenStream, _input: TokenStream) -> TokenStream {
419427
}
420428
};
421429

422-
let token = if cfg!(feature = "is_sync") {
423-
quote!()
424-
} else {
430+
if is_async!() {
425431
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+
}
429436
}
430437

431438
macro_rules! match_nested_meta_to_str_lit {

0 commit comments

Comments
 (0)