]> Untitled Git - bdk/commitdiff
Don't default to use `async`/`await` on `wasm32`
authorElias Rohrer <ero@tnull.de>
Tue, 10 Jan 2023 14:05:55 +0000 (15:05 +0100)
committerElias Rohrer <ero@tnull.de>
Fri, 27 Jan 2023 17:09:26 +0000 (11:09 -0600)
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
macros/src/lib.rs
src/blockchain/mod.rs
src/lib.rs

index 82157ffb1aa3e5dcdd8f4ad8b370ed2aa47d1763..e7def9d09f20e5bb3ee0f9c1c8fa3ad24333fa7d 100644 (file)
@@ -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
index 74eda5cf4e46132bcaac4663ae6aa8230280dd50..2fabf2cfcddb529585cdeb324464d15013c2d6e0 100644 (file)
@@ -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
             }
index 84a8c2e395502e1ed7d1c46dc303e39839ba1523..13c0b2a4282ddf4b32a36462a370e2d92a479f58 100644 (file)
@@ -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<D: BatchDatabase>(
         &self,
         wallet: &Wallet<D>,
index 23c4e34dce597b1b9ffbd38e642ab7c886d6cb8a..0cdea4a529f70a5a2bbbf8f70b5682aad11bff3c 100644 (file)
@@ -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]