From 32912eaa05fd3bb4fa1577ca413dcedf909544d9 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 10 Jan 2023 15:05:55 +0100 Subject: [PATCH] Don't default to use `async`/`await` on `wasm32` We don't automatically want to make the interface `async` based on the used architecture, but now require the user to explicitly set the `async-interface` feature. --- .github/workflows/cont_integration.yml | 2 +- macros/src/lib.rs | 28 +++++++++++++------------- src/blockchain/mod.rs | 7 ++----- src/lib.rs | 2 +- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/.github/workflows/cont_integration.yml b/.github/workflows/cont_integration.yml index 82157ffb1..e7def9d09 100644 --- a/.github/workflows/cont_integration.yml +++ b/.github/workflows/cont_integration.yml @@ -154,7 +154,7 @@ jobs: - name: Update toolchain run: rustup update - name: Check - run: cargo check --target wasm32-unknown-unknown --features use-esplora-async,dev-getrandom-wasm --no-default-features + run: cargo check --target wasm32-unknown-unknown --features async-interface,use-esplora-async,dev-getrandom-wasm --no-default-features fmt: name: Rust fmt diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 74eda5cf4..2fabf2cfc 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -19,7 +19,7 @@ use syn::{parse, ImplItemMethod, ItemImpl, ItemTrait, Token}; fn add_async_trait(mut parsed: ItemTrait) -> TokenStream { let output = quote! { - #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))] + #[cfg(not(feature = "async-interface"))] #parsed }; @@ -32,7 +32,7 @@ fn add_async_trait(mut parsed: ItemTrait) -> TokenStream { let output = quote! { #output - #[cfg(any(target_arch = "wasm32", feature = "async-interface"))] + #[cfg(feature = "async-interface")] #[async_trait(?Send)] #parsed }; @@ -42,7 +42,7 @@ fn add_async_trait(mut parsed: ItemTrait) -> TokenStream { fn add_async_method(mut parsed: ImplItemMethod) -> TokenStream { let output = quote! { - #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))] + #[cfg(not(feature = "async-interface"))] #parsed }; @@ -51,7 +51,7 @@ fn add_async_method(mut parsed: ImplItemMethod) -> TokenStream { let output = quote! { #output - #[cfg(any(target_arch = "wasm32", feature = "async-interface"))] + #[cfg(feature = "async-interface")] #parsed }; @@ -60,7 +60,7 @@ fn add_async_method(mut parsed: ImplItemMethod) -> TokenStream { fn add_async_impl_trait(mut parsed: ItemImpl) -> TokenStream { let output = quote! { - #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))] + #[cfg(not(feature = "async-interface"))] #parsed }; @@ -73,7 +73,7 @@ fn add_async_impl_trait(mut parsed: ItemImpl) -> TokenStream { let output = quote! { #output - #[cfg(any(target_arch = "wasm32", feature = "async-interface"))] + #[cfg(feature = "async-interface")] #[async_trait(?Send)] #parsed }; @@ -81,7 +81,7 @@ fn add_async_impl_trait(mut parsed: ItemImpl) -> TokenStream { output.into() } -/// Makes a method or every method of a trait "async" only if the target_arch is "wasm32" +/// Makes a method or every method of a trait `async`, if the `async-interface` feature is enabled. /// /// Requires the `async-trait` crate as a dependency whenever this attribute is used on a trait /// definition or trait implementation. @@ -101,18 +101,18 @@ pub fn maybe_async(_attr: TokenStream, item: TokenStream) -> TokenStream { } } -/// Awaits if target_arch is "wasm32", does nothing otherwise +/// Awaits, if the `async-interface` feature is enabled. #[proc_macro] pub fn maybe_await(expr: TokenStream) -> TokenStream { let expr: proc_macro2::TokenStream = expr.into(); let quoted = quote! { { - #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))] + #[cfg(not(feature = "async-interface"))] { #expr } - #[cfg(any(target_arch = "wasm32", feature = "async-interface"))] + #[cfg(feature = "async-interface")] { #expr.await } @@ -122,20 +122,20 @@ pub fn maybe_await(expr: TokenStream) -> TokenStream { quoted.into() } -/// Awaits if target_arch is "wasm32", uses `tokio::Runtime::block_on()` otherwise +/// Awaits, if the `async-interface` feature is enabled, uses `tokio::Runtime::block_on()` otherwise /// -/// Requires the `tokio` crate as a dependecy with `rt-core` or `rt-threaded` to build on non-wasm32 platforms. +/// Requires the `tokio` crate as a dependecy with `rt-core` or `rt-threaded` to build. #[proc_macro] pub fn await_or_block(expr: TokenStream) -> TokenStream { let expr: proc_macro2::TokenStream = expr.into(); let quoted = quote! { { - #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))] + #[cfg(not(feature = "async-interface"))] { tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(#expr) } - #[cfg(any(target_arch = "wasm32", feature = "async-interface"))] + #[cfg(feature = "async-interface")] { #expr.await } diff --git a/src/blockchain/mod.rs b/src/blockchain/mod.rs index 84a8c2e39..13c0b2a42 100644 --- a/src/blockchain/mod.rs +++ b/src/blockchain/mod.rs @@ -249,11 +249,8 @@ pub trait BlockchainFactory { /// operations to build a blockchain for a given wallet, so if a wallet needs to be synced /// often it's recommended to use [`BlockchainFactory::build_for_wallet`] to reuse the same /// blockchain multiple times. - #[cfg(not(any(target_arch = "wasm32", feature = "async-interface")))] - #[cfg_attr( - docsrs, - doc(cfg(not(any(target_arch = "wasm32", feature = "async-interface")))) - )] + #[cfg(not(feature = "async-interface"))] + #[cfg_attr(docsrs, doc(cfg(not(feature = "async-interface"))))] fn sync_wallet( &self, wallet: &Wallet, diff --git a/src/lib.rs b/src/lib.rs index 23c4e34dc..0cdea4a52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,7 +227,7 @@ compile_error!( #[cfg(feature = "keys-bip39")] extern crate bip39; -#[cfg(any(target_arch = "wasm32", feature = "async-interface"))] +#[cfg(feature = "async-interface")] #[macro_use] extern crate async_trait; #[macro_use]