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]
esplora = ["reqwest", "futures"]
key-value-db = ["sled"]
cli-utils = ["clap", "base64"]
+async-interface = ["async-trait"]
[dev-dependencies]
lazy_static = "1.4"
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
};
let output = quote! {
#output
- #[cfg(target_arch = "wasm32")]
+ #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
#[async_trait(?Send)]
#parsed
};
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
};
let output = quote! {
#output
- #[cfg(target_arch = "wasm32")]
+ #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
#parsed
};
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
};
let output = quote! {
#output
- #[cfg(target_arch = "wasm32")]
+ #[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
#[async_trait(?Send)]
#parsed
};
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
}
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]
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
}