Skip to content

Commit ba39034

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 ba39034

File tree

3 files changed

+16
-4
lines changed

3 files changed

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

+5-1
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
21+
by 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
@@ -171,7 +173,7 @@ blocking code except for async/await keywords. And use feature gate
171173
}
172174
```
173175

174-
### What's Under the Hook
176+
### What's Under the Hood
175177

176178
`maybe-async` compiles your code in different way with the `is_sync` feature
177179
gate. It removes all `await` and `async` keywords in your code under
@@ -277,6 +279,8 @@ fn maybe_async_fn() -> Result<(), ()> {
277279
}
278280
```
279281

282+
### Default to Sync
283+
280284
### Examples
281285

282286
#### rust client for services

src/lib.rs

+9-3
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
19+
//! by 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,7 +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") {
434+
let token = if is_sync() {
433435
quote!(#input)
434436
} else {
435437
quote!()
@@ -447,7 +449,7 @@ pub fn async_impl(args: TokenStream, _input: TokenStream) -> TokenStream {
447449
Ok(m) => m,
448450
Err(e) => return e.to_compile_error().into(),
449451
};
450-
let token = if cfg!(feature = "is_sync") {
452+
let token = if is_sync() {
451453
quote!()
452454
} else {
453455
let mut item = parse_macro_input!(_input as Item);
@@ -466,6 +468,10 @@ fn parse_nested_meta_or_str(input: ParseStream) -> Result<TokenStream2> {
466468
}
467469
}
468470

471+
fn is_sync() -> bool {
472+
cfg!(feature = "is_sync") || (cfg!(feature = "default_sync") && !cfg!(feature = "is_async"))
473+
}
474+
469475
/// Handy macro to unify test code of sync and async code
470476
///
471477
/// Since the API of both sync and async code are the same,

0 commit comments

Comments
 (0)