Skip to content

feat: add default_sync and is_async feature gate #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ async-trait = "0.1"
[features]
default = [ ]
is_sync = [ ]
default_sync = [ ]
is_async = [ ]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ macro**.
those `async` and `await` when you need a blocking code.
- Switch between sync and async by toggling `is_sync` feature gate in
`Cargo.toml`.
- Alternatively activating the `default_sync` feature will switch to sync by
default and can be switched to async via `is_async`.
- use `must_be_async` and `must_be_sync` to keep code in specified version
- use `async_impl` and `sync_impl` to only compile code block on specified
version
Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//! those `async` and `await` when you need a blocking code.
//! - Switch between sync and async by toggling `is_sync` feature gate in
//! `Cargo.toml`.
//! - Alternatively activating the `default_sync` feature will switch to sync by
//! default and can be switched to async via `is_async`.
//! - use `must_be_async` and `must_be_sync` to keep code in specified version
//! - use `async_impl` and `sync_impl` to only compile code block on specified
//! version
Expand Down Expand Up @@ -396,7 +398,7 @@ pub fn maybe_async(args: TokenStream, input: TokenStream) -> TokenStream {
};
let mut item = parse_macro_input!(input as Item);

let token = if cfg!(feature = "is_sync") {
let token = if is_sync() {
convert_sync(&mut item)
} else {
convert_async(&mut item, mode)
Expand Down Expand Up @@ -429,11 +431,7 @@ pub fn must_be_sync(_args: TokenStream, input: TokenStream) -> TokenStream {
#[proc_macro_attribute]
pub fn sync_impl(_args: TokenStream, input: TokenStream) -> TokenStream {
let input = TokenStream2::from(input);
let token = if cfg!(feature = "is_sync") {
quote!(#input)
} else {
quote!()
};
let token = if is_sync() { quote!(#input) } else { quote!() };
token.into()
}

Expand All @@ -447,7 +445,7 @@ pub fn async_impl(args: TokenStream, _input: TokenStream) -> TokenStream {
Ok(m) => m,
Err(e) => return e.to_compile_error().into(),
};
let token = if cfg!(feature = "is_sync") {
let token = if is_sync() {
quote!()
} else {
let mut item = parse_macro_input!(_input as Item);
Expand All @@ -466,6 +464,10 @@ fn parse_nested_meta_or_str(input: ParseStream) -> Result<TokenStream2> {
}
}

fn is_sync() -> bool {
cfg!(feature = "is_sync") || (cfg!(feature = "default_sync") && !cfg!(feature = "is_async"))
}

/// Handy macro to unify test code of sync and async code
///
/// Since the API of both sync and async code are the same,
Expand Down