diff --git a/Cargo.toml b/Cargo.toml index add69f35afa..f0f09f547f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ members = [ "lightning-rapid-gossip-sync", "lightning-custom-message", "lightning-transaction-sync", + "lightning-macros", "possiblyrandom", ] diff --git a/ci/ci-tests.sh b/ci/ci-tests.sh index 47f0621683c..c67f04c41e0 100755 --- a/ci/ci-tests.sh +++ b/ci/ci-tests.sh @@ -46,6 +46,7 @@ WORKSPACE_MEMBERS=( lightning-rapid-gossip-sync lightning-custom-message lightning-transaction-sync + lightning-macros possiblyrandom ) diff --git a/lightning-macros/Cargo.toml b/lightning-macros/Cargo.toml new file mode 100644 index 00000000000..480d8f0f7d8 --- /dev/null +++ b/lightning-macros/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "lightning-macros" +version = "0.1.0" +authors = ["Elias Rohrer"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/lightningdevkit/rust-lightning/" +description = """ +Proc macros used by LDK +""" +edition = "2021" + +[package.metadata.docs.rs] +rustdoc-args = ["--cfg", "docsrs"] + +[lib] +proc-macro = true + +[features] + +[dependencies] +syn = { version = "2.0.77", default-features = false, features = ["parsing", "printing", "proc-macro", "full"] } +proc-macro2 = { version = "1.0.86", default-features = false, features = ["proc-macro"] } +quote = { version = "1.0", default-features = false, features = ["proc-macro"] } + +[lints] +workspace = true diff --git a/lightning-macros/src/lib.rs b/lightning-macros/src/lib.rs new file mode 100644 index 00000000000..fd4707e5856 --- /dev/null +++ b/lightning-macros/src/lib.rs @@ -0,0 +1,76 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +#![crate_name = "lightning_macros"] + +//! Proc macros used by LDK + +#![cfg_attr(not(test), no_std)] +#![deny(missing_docs)] +#![forbid(unsafe_code)] +#![deny(rustdoc::broken_intra_doc_links)] +#![deny(rustdoc::private_intra_doc_links)] +#![cfg_attr(docsrs, feature(doc_auto_cfg))] + +use proc_macro::TokenStream; +use quote::quote; +use syn::spanned::Spanned; +use syn::{parse, ImplItemFn, Token}; + +fn add_async_method(mut parsed: ImplItemFn) -> TokenStream { + let output = quote! { + #[cfg(not(feature = "async-interface"))] + #parsed + }; + + parsed.sig.asyncness = Some(Token![async](parsed.span())); + + let output = quote! { + #output + + #[cfg(feature = "async-interface")] + #parsed + }; + + output.into() +} + +/// Makes a method `async`, if the `async-interface` feature is enabled. +#[proc_macro_attribute] +pub fn maybe_async(_attr: TokenStream, item: TokenStream) -> TokenStream { + if let Ok(parsed) = parse(item) { + add_async_method(parsed) + } else { + (quote! { + compile_error!("#[maybe_async] can only be used on methods") + }) + .into() + } +} + +/// 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(not(feature = "async-interface"))] + { + #expr + } + + #[cfg(feature = "async-interface")] + { + #expr.await + } + } + }; + + quoted.into() +} diff --git a/lightning-transaction-sync/Cargo.toml b/lightning-transaction-sync/Cargo.toml index 34105ea3e90..34ddfec6f0f 100644 --- a/lightning-transaction-sync/Cargo.toml +++ b/lightning-transaction-sync/Cargo.toml @@ -24,8 +24,8 @@ async-interface = [] [dependencies] lightning = { version = "0.0.124", path = "../lightning", default-features = false, features = ["std"] } +lightning-macros = { version = "0.1", path = "../lightning-macros", default-features = false } bitcoin = { version = "0.32.2", default-features = false } -bdk-macros = "0.6" futures = { version = "0.3", optional = true } esplora-client = { version = "0.9", default-features = false, optional = true } electrum-client = { version = "0.21.0", optional = true } diff --git a/lightning-transaction-sync/src/esplora.rs b/lightning-transaction-sync/src/esplora.rs index 8b8b39cfb4f..a191260bc01 100644 --- a/lightning-transaction-sync/src/esplora.rs +++ b/lightning-transaction-sync/src/esplora.rs @@ -13,6 +13,8 @@ use lightning::chain::{Confirm, Filter}; use lightning::util::logger::Logger; use lightning::{log_debug, log_error, log_trace}; +use lightning_macros::{maybe_async, maybe_await}; + use bitcoin::{BlockHash, Script, Txid}; #[cfg(not(feature = "async-interface"))] diff --git a/lightning-transaction-sync/src/lib.rs b/lightning-transaction-sync/src/lib.rs index 9c6831cef5d..19ebe007ec2 100644 --- a/lightning-transaction-sync/src/lib.rs +++ b/lightning-transaction-sync/src/lib.rs @@ -71,10 +71,6 @@ #![deny(unsafe_code)] #![cfg_attr(docsrs, feature(doc_auto_cfg))] -#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] -#[macro_use] -extern crate bdk_macros; - #[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] mod esplora; diff --git a/lightning-transaction-sync/tests/integration_tests.rs b/lightning-transaction-sync/tests/integration_tests.rs index c887b2be0bf..0a6f40b0972 100644 --- a/lightning-transaction-sync/tests/integration_tests.rs +++ b/lightning-transaction-sync/tests/integration_tests.rs @@ -11,7 +11,8 @@ use lightning_transaction_sync::ElectrumSyncClient; #[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] use lightning_transaction_sync::EsploraSyncClient; -use bdk_macros::maybe_await; +use lightning_macros::maybe_await; + use bitcoin::block::Header; use bitcoin::constants::genesis_block; use bitcoin::network::Network;