Skip to content

Commit 3eb1ae7

Browse files
committed
feat: add default_sync and is_async feature gate
Purpose of these features is to enable depdendants to provide a feature to enable async support instead of a feature to disable it. Creates (which would want to be sync by default) could then: * Always enable the `default_sync` feature. * Enable async support with the `is_async` feature when needed.
1 parent b32a817 commit 3eb1ae7

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ async-trait = "0.1"
4343
[features]
4444
default = [ ]
4545
is_sync = [ ]
46+
default_sync = [ ]
47+
is_async = [ ]

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ macro**.
1717
those `async` and `await` when you need a blocking code.
1818
- Switch between sync and async by toggling `is_sync` feature gate in
1919
`Cargo.toml`.
20+
- Alternatively activating the `default_sync` feature will switch to sync by
21+
default and can be switched to async via `is_async`.
2022
- use `must_be_async` and `must_be_sync` to keep code in specified version
2123
- use `async_impl` and `sync_impl` to only compile code block on specified
2224
version

src/lib.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
//! those `async` and `await` when you need a blocking code.
1616
//! - Switch between sync and async by toggling `is_sync` feature gate in
1717
//! `Cargo.toml`.
18+
//! - Alternatively activating the `default_sync` feature will switch to sync by
19+
//! default and can be switched to async via `is_async`.
1820
//! - use `must_be_async` and `must_be_sync` to keep code in specified version
1921
//! - use `async_impl` and `sync_impl` to only compile code block on specified
2022
//! version
@@ -396,7 +398,7 @@ pub fn maybe_async(args: TokenStream, input: TokenStream) -> TokenStream {
396398
};
397399
let mut item = parse_macro_input!(input as Item);
398400

399-
let token = if cfg!(feature = "is_sync") {
401+
let token = if is_sync() {
400402
convert_sync(&mut item)
401403
} else {
402404
convert_async(&mut item, mode)
@@ -429,11 +431,7 @@ pub fn must_be_sync(_args: TokenStream, input: TokenStream) -> TokenStream {
429431
#[proc_macro_attribute]
430432
pub fn sync_impl(_args: TokenStream, input: TokenStream) -> TokenStream {
431433
let input = TokenStream2::from(input);
432-
let token = if cfg!(feature = "is_sync") {
433-
quote!(#input)
434-
} else {
435-
quote!()
436-
};
434+
let token = if is_sync() { quote!(#input) } else { quote!() };
437435
token.into()
438436
}
439437

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

467+
fn is_sync() -> bool {
468+
cfg!(feature = "is_sync") || (cfg!(feature = "default_sync") && !cfg!(feature = "is_async"))
469+
}
470+
469471
/// Handy macro to unify test code of sync and async code
470472
///
471473
/// Since the API of both sync and async code are the same,

0 commit comments

Comments
 (0)