From 871f4398b95018eb657eff40d389aada0466a401 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 16 Jul 2024 08:30:28 +1000 Subject: [PATCH 1/2] Add optional comma to function parameter list The `define_extension_trait` macro currently does not allow for a trailing comma in the function paramater list, this occurs when the function paramaters are put on individual lines (eg by `rustfmt`). Extend the macro to support function paramaters being on individual lines. This should have been done originally instead of my manual override of formatting - bad Tobin no biscuit. --- bitcoin/src/internal_macros.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs index a93dbcb9f..5d90b9592 100644 --- a/bitcoin/src/internal_macros.rs +++ b/bitcoin/src/internal_macros.rs @@ -218,7 +218,7 @@ macro_rules! define_extension_trait { ($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident { $( $(#[$($fn_attrs:tt)*])* - fn $fn:ident($slf:ident $(, $param_name:ident: $param_type:ty)*) $( -> $ret:ty )? $body:block + fn $fn:ident($slf:ident $(, $param_name:ident: $param_type:ty)* $(,)?) $( -> $ret:ty )? $body:block )* }) => { $(#[$($trait_attrs)*])* $trait_vis trait $trait_name { @@ -238,7 +238,7 @@ macro_rules! define_extension_trait { ($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident { $( $(#[$($fn_attrs:tt)*])* - fn $fn:ident(&$slf:ident $(, $param_name:ident: $param_type:ty)*) $( -> $ret:ty )? $body:block + fn $fn:ident(&$slf:ident $(, $param_name:ident: $param_type:ty)* $(,)?) $( -> $ret:ty )? $body:block )* }) => { $(#[$($trait_attrs)*])* $trait_vis trait $trait_name { @@ -258,7 +258,7 @@ macro_rules! define_extension_trait { ($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident { $( $(#[$($fn_attrs:tt)*])* - fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?(&$slf:ident $(, $param_name:ident: $param_type:ty)*) $( -> $ret:ty )? $body:block + fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?(&$slf:ident $(, $param_name:ident: $param_type:ty)* $(,)?) $( -> $ret:ty )? $body:block )* }) => { $(#[$($trait_attrs)*])* $trait_vis trait $trait_name { @@ -278,7 +278,7 @@ macro_rules! define_extension_trait { ($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident { $( $(#[$($fn_attrs:tt)*])* - fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?($($param_name:ident: $param_type:ty),*) $( -> $ret:ty )? $body:block + fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?($($param_name:ident: $param_type:ty),* $(,)?) $( -> $ret:ty )? $body:block )* }) => { $(#[$($trait_attrs)*])* $trait_vis trait $trait_name { @@ -298,7 +298,7 @@ macro_rules! define_extension_trait { ($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident { $( $(#[$($fn_attrs:tt)*])* - fn $fn:ident>($($param_name:ident: $param_type:ty),*) $( -> $ret:ty )? $body:block + fn $fn:ident>($($param_name:ident: $param_type:ty),* $(,)?) $( -> $ret:ty )? $body:block )* }) => { $(#[$($trait_attrs)*])* $trait_vis trait $trait_name { From 386ad93253b7ee7421593b29c3a4129e9fcb8b86 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 16 Jul 2024 08:35:04 +1000 Subject: [PATCH 2/2] Manually format function parameters Now that the `define_extension_trait` can handle function parameters on individual lines revert the manual formatting that was introduced in PR #2955. Refactor only, no logic changes. --- bitcoin/src/address/script_pubkey.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/address/script_pubkey.rs b/bitcoin/src/address/script_pubkey.rs index ff38e080b..de10c5f61 100644 --- a/bitcoin/src/address/script_pubkey.rs +++ b/bitcoin/src/address/script_pubkey.rs @@ -48,7 +48,11 @@ define_extension_trait! { /// Computes P2TR output with a given internal key and a single script spending path equal to /// the current script, assuming that the script is a Tapscript. - fn to_p2tr(&self, secp: &Secp256k1, internal_key: UntweakedPublicKey) -> ScriptBuf { + fn to_p2tr( + &self, + secp: &Secp256k1, + internal_key: UntweakedPublicKey, + ) -> ScriptBuf { let leaf_hash = self.tapscript_leaf_hash(); let merkle_root = TapNodeHash::from(leaf_hash); ScriptBuf::new_p2tr(secp, internal_key, Some(merkle_root)) @@ -150,7 +154,11 @@ define_extension_trait! { /// Generates P2TR for script spending path using an internal public key and some optional /// script tree Merkle root. - fn new_p2tr(secp: &Secp256k1, internal_key: UntweakedPublicKey, merkle_root: Option) -> Self { + fn new_p2tr( + secp: &Secp256k1, + internal_key: UntweakedPublicKey, + merkle_root: Option, + ) -> Self { let (output_key, _) = internal_key.tap_tweak(secp, merkle_root); // output key is 32 bytes long, so it's safe to use `new_witness_program_unchecked` (Segwitv1) new_witness_program_unchecked(WitnessVersion::V1, output_key.serialize())