Skip to content

Commit 81923c7

Browse files
authored
Merge pull request #3330 from tnull/2024-09-add-macros-crate
Add `lightning-macros` crate and drop `bdk_macros` dependency
2 parents 4a7b01a + 99ae425 commit 81923c7

File tree

8 files changed

+109
-6
lines changed

8 files changed

+109
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ members = [
1414
"lightning-rapid-gossip-sync",
1515
"lightning-custom-message",
1616
"lightning-transaction-sync",
17+
"lightning-macros",
1718
"possiblyrandom",
1819
]
1920

ci/ci-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ WORKSPACE_MEMBERS=(
4646
lightning-rapid-gossip-sync
4747
lightning-custom-message
4848
lightning-transaction-sync
49+
lightning-macros
4950
possiblyrandom
5051
)
5152

lightning-macros/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "lightning-macros"
3+
version = "0.1.0"
4+
authors = ["Elias Rohrer"]
5+
license = "MIT OR Apache-2.0"
6+
repository = "https://github.com/lightningdevkit/rust-lightning/"
7+
description = """
8+
Proc macros used by LDK
9+
"""
10+
edition = "2021"
11+
12+
[package.metadata.docs.rs]
13+
rustdoc-args = ["--cfg", "docsrs"]
14+
15+
[lib]
16+
proc-macro = true
17+
18+
[features]
19+
20+
[dependencies]
21+
syn = { version = "2.0.77", default-features = false, features = ["parsing", "printing", "proc-macro", "full"] }
22+
proc-macro2 = { version = "1.0.86", default-features = false, features = ["proc-macro"] }
23+
quote = { version = "1.0", default-features = false, features = ["proc-macro"] }
24+
25+
[lints]
26+
workspace = true

lightning-macros/src/lib.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
#![crate_name = "lightning_macros"]
11+
12+
//! Proc macros used by LDK
13+
14+
#![cfg_attr(not(test), no_std)]
15+
#![deny(missing_docs)]
16+
#![forbid(unsafe_code)]
17+
#![deny(rustdoc::broken_intra_doc_links)]
18+
#![deny(rustdoc::private_intra_doc_links)]
19+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
20+
21+
use proc_macro::TokenStream;
22+
use quote::quote;
23+
use syn::spanned::Spanned;
24+
use syn::{parse, ImplItemFn, Token};
25+
26+
fn add_async_method(mut parsed: ImplItemFn) -> TokenStream {
27+
let output = quote! {
28+
#[cfg(not(feature = "async-interface"))]
29+
#parsed
30+
};
31+
32+
parsed.sig.asyncness = Some(Token![async](parsed.span()));
33+
34+
let output = quote! {
35+
#output
36+
37+
#[cfg(feature = "async-interface")]
38+
#parsed
39+
};
40+
41+
output.into()
42+
}
43+
44+
/// Makes a method `async`, if the `async-interface` feature is enabled.
45+
#[proc_macro_attribute]
46+
pub fn maybe_async(_attr: TokenStream, item: TokenStream) -> TokenStream {
47+
if let Ok(parsed) = parse(item) {
48+
add_async_method(parsed)
49+
} else {
50+
(quote! {
51+
compile_error!("#[maybe_async] can only be used on methods")
52+
})
53+
.into()
54+
}
55+
}
56+
57+
/// Awaits, if the `async-interface` feature is enabled.
58+
#[proc_macro]
59+
pub fn maybe_await(expr: TokenStream) -> TokenStream {
60+
let expr: proc_macro2::TokenStream = expr.into();
61+
let quoted = quote! {
62+
{
63+
#[cfg(not(feature = "async-interface"))]
64+
{
65+
#expr
66+
}
67+
68+
#[cfg(feature = "async-interface")]
69+
{
70+
#expr.await
71+
}
72+
}
73+
};
74+
75+
quoted.into()
76+
}

lightning-transaction-sync/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ async-interface = []
2424

2525
[dependencies]
2626
lightning = { version = "0.0.124", path = "../lightning", default-features = false, features = ["std"] }
27+
lightning-macros = { version = "0.1", path = "../lightning-macros", default-features = false }
2728
bitcoin = { version = "0.32.2", default-features = false }
28-
bdk-macros = "0.6"
2929
futures = { version = "0.3", optional = true }
3030
esplora-client = { version = "0.9", default-features = false, optional = true }
3131
electrum-client = { version = "0.21.0", optional = true }

lightning-transaction-sync/src/esplora.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use lightning::chain::{Confirm, Filter};
1313
use lightning::util::logger::Logger;
1414
use lightning::{log_debug, log_error, log_trace};
1515

16+
use lightning_macros::{maybe_async, maybe_await};
17+
1618
use bitcoin::{BlockHash, Script, Txid};
1719

1820
#[cfg(not(feature = "async-interface"))]

lightning-transaction-sync/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@
7171
#![deny(unsafe_code)]
7272
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
7373

74-
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
75-
#[macro_use]
76-
extern crate bdk_macros;
77-
7874
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
7975
mod esplora;
8076

lightning-transaction-sync/tests/integration_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use lightning_transaction_sync::ElectrumSyncClient;
1111
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
1212
use lightning_transaction_sync::EsploraSyncClient;
1313

14-
use bdk_macros::maybe_await;
14+
use lightning_macros::maybe_await;
15+
1516
use bitcoin::block::Header;
1617
use bitcoin::constants::genesis_block;
1718
use bitcoin::network::Network;

0 commit comments

Comments
 (0)