]> Untitled Git - bdk/commitdiff
Add a feature to enable the async interface on non-wasm32 platforms
authorAlekos Filini <alekos.filini@gmail.com>
Mon, 10 Aug 2020 09:41:19 +0000 (11:41 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Mon, 10 Aug 2020 09:41:19 +0000 (11:41 +0200)
Follow-up to: #28

Cargo.toml
macros/src/lib.rs
src/lib.rs

index ee6d1216cdc2353c171dc63105df20de2626e21f..5896b05f85c38004ef4bd4bca7fdffadf13c87a7 100644 (file)
@@ -20,6 +20,7 @@ reqwest = { version = "0.10", optional = true, features = ["json"] }
 futures = { version = "0.3", optional = true }
 clap = { version = "2.33", optional = true }
 base64 = { version = "^0.11", optional = true }
+async-trait = { version = "0.1", optional = true }
 
 # Platform-specific dependencies
 [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
@@ -37,6 +38,7 @@ electrum = ["electrum-client"]
 esplora = ["reqwest", "futures"]
 key-value-db = ["sled"]
 cli-utils = ["clap", "base64"]
+async-interface = ["async-trait"]
 
 [dev-dependencies]
 lazy_static = "1.4"
index c7d984d6bcc93eda821ccd268f624b2493be89db..bd74a521f27202a10bbffa767b9d1c9a4d433c29 100644 (file)
@@ -8,7 +8,7 @@ use syn::{parse, ImplItemMethod, ItemImpl, ItemTrait, Token};
 
 fn add_async_trait(mut parsed: ItemTrait) -> TokenStream {
     let output = quote! {
-        #[cfg(not(target_arch = "wasm32"))]
+        #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))]
         #parsed
     };
 
@@ -21,7 +21,7 @@ fn add_async_trait(mut parsed: ItemTrait) -> TokenStream {
     let output = quote! {
         #output
 
-        #[cfg(target_arch = "wasm32")]
+        #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
         #[async_trait(?Send)]
         #parsed
     };
@@ -31,7 +31,7 @@ fn add_async_trait(mut parsed: ItemTrait) -> TokenStream {
 
 fn add_async_method(mut parsed: ImplItemMethod) -> TokenStream {
     let output = quote! {
-        #[cfg(not(target_arch = "wasm32"))]
+        #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))]
         #parsed
     };
 
@@ -40,7 +40,7 @@ fn add_async_method(mut parsed: ImplItemMethod) -> TokenStream {
     let output = quote! {
         #output
 
-        #[cfg(target_arch = "wasm32")]
+        #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
         #parsed
     };
 
@@ -49,7 +49,7 @@ fn add_async_method(mut parsed: ImplItemMethod) -> TokenStream {
 
 fn add_async_impl_trait(mut parsed: ItemImpl) -> TokenStream {
     let output = quote! {
-        #[cfg(not(target_arch = "wasm32"))]
+        #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))]
         #parsed
     };
 
@@ -62,7 +62,7 @@ fn add_async_impl_trait(mut parsed: ItemImpl) -> TokenStream {
     let output = quote! {
         #output
 
-        #[cfg(target_arch = "wasm32")]
+        #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
         #[async_trait(?Send)]
         #parsed
     };
@@ -95,12 +95,12 @@ pub fn maybe_await(expr: TokenStream) -> TokenStream {
     let expr: proc_macro2::TokenStream = expr.into();
     let quoted = quote! {
         {
-            #[cfg(not(target_arch = "wasm32"))]
+            #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))]
             {
                 #expr
             }
 
-            #[cfg(target_arch = "wasm32")]
+            #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
             {
                 #expr.await
             }
@@ -110,7 +110,7 @@ pub fn maybe_await(expr: TokenStream) -> TokenStream {
     quoted.into()
 }
 
-/// Awaits if target_arch is "wasm32", uses `futures::executor::block_on()` otherwise
+/// Awaits if target_arch is "wasm32", 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.
 #[proc_macro]
@@ -118,12 +118,12 @@ pub fn await_or_block(expr: TokenStream) -> TokenStream {
     let expr: proc_macro2::TokenStream = expr.into();
     let quoted = quote! {
         {
-            #[cfg(not(target_arch = "wasm32"))]
+            #[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))]
             {
                 tokio::runtime::Runtime::new().unwrap().block_on(#expr)
             }
 
-            #[cfg(target_arch = "wasm32")]
+            #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
             {
                 #expr.await
             }
index 96dcc6bc06703a6a964d3dc565dfdb23091bba3d..4629ec773e83c9c1cb8b2b2e852bab8347b1480e 100644 (file)
@@ -5,7 +5,7 @@ extern crate serde;
 #[macro_use]
 extern crate serde_json;
 
-#[cfg(target_arch = "wasm32")]
+#[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
 #[macro_use]
 extern crate async_trait;
 #[macro_use]