Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lightning-macros crate and drop bdk_macros dependency #3330

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"lightning-rapid-gossip-sync",
"lightning-custom-message",
"lightning-transaction-sync",
"lightning-macros",
"possiblyrandom",
]

Expand Down
1 change: 1 addition & 0 deletions ci/ci-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ WORKSPACE_MEMBERS=(
lightning-rapid-gossip-sync
lightning-custom-message
lightning-transaction-sync
lightning-macros
possiblyrandom
)

Expand Down
26 changes: 26 additions & 0 deletions lightning-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
129 changes: 129 additions & 0 deletions lightning-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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, ItemImpl, ItemTrait, Token};

fn add_async_trait(mut parsed: ItemTrait) -> TokenStream {
let output = quote! {
#[cfg(not(feature = "async-interface"))]
#parsed
};

for mut item in &mut parsed.items {
if let syn::TraitItem::Fn(f) = &mut item {
f.sig.asyncness = Some(Token![async](f.span()));
}
}

let output = quote! {
#output

#[cfg(feature = "async-interface")]
#[async_trait(?Send)]
#parsed
};

output.into()
}

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()
}

fn add_async_impl_trait(mut parsed: ItemImpl) -> TokenStream {
let output = quote! {
#[cfg(not(feature = "async-interface"))]
#parsed
};

for mut item in &mut parsed.items {
if let syn::ImplItem::Fn(f) = &mut item {
f.sig.asyncness = Some(Token![async](f.span()));
}
}

let output = quote! {
#output

#[cfg(feature = "async-interface")]
#[async_trait(?Send)]
#parsed
};

output.into()
}

/// 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.
#[proc_macro_attribute]
pub fn maybe_async(_attr: TokenStream, item: TokenStream) -> TokenStream {
if let Ok(parsed) = parse(item.clone()) {
add_async_trait(parsed)
} else if let Ok(parsed) = parse(item.clone()) {
add_async_method(parsed)
} else if let Ok(parsed) = parse(item) {
add_async_impl_trait(parsed)
} else {
(quote! {
compile_error!("#[maybe_async] can only be used on methods, trait or trait impl blocks")
})
.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()
}
2 changes: 1 addition & 1 deletion lightning-transaction-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions lightning-transaction-sync/src/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
4 changes: 0 additions & 4 deletions lightning-transaction-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion lightning-transaction-sync/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading