From 27adc09e9fcaf868b3aa55b5dde140203281769a Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Sun, 11 Aug 2024 17:49:33 +0200 Subject: [PATCH] Generalize fn params in `define_extension_trait` The macro was trying to "parse" the parameters of functions defined in extension trait. This was not needed and it was causing problems around the `self` parameter. In this commit we change the macro to just pass the parameters through. --- bitcoin/src/address/script_pubkey.rs | 18 +++++++++--------- bitcoin/src/consensus_validation.rs | 4 ++-- bitcoin/src/internal_macros.rs | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bitcoin/src/address/script_pubkey.rs b/bitcoin/src/address/script_pubkey.rs index e299bed78..89b7298aa 100644 --- a/bitcoin/src/address/script_pubkey.rs +++ b/bitcoin/src/address/script_pubkey.rs @@ -22,7 +22,7 @@ define_extension_trait! { /// Extension functionality to add scriptPubkey support to the [`Builder`] type. pub trait BuilderExt impl for Builder { /// Adds instructions to push a public key onto the stack. - fn push_key(self: Self, key: PublicKey) -> Builder { + fn push_key(self, key: PublicKey) -> Builder { if key.compressed { self.push_slice(key.inner.serialize()) } else { @@ -31,7 +31,7 @@ define_extension_trait! { } /// Adds instructions to push an XOnly public key onto the stack. - fn push_x_only_key(self: Self, x_only_key: XOnlyPublicKey) -> Builder { + fn push_x_only_key(self, x_only_key: XOnlyPublicKey) -> Builder { self.push_slice(x_only_key.serialize()) } } @@ -42,14 +42,14 @@ define_extension_trait! { pub trait ScriptExt impl for Script { /// Computes the P2WSH output corresponding to this witnessScript (aka the "witness redeem /// script"). - fn to_p2wsh(self: &Self) -> Result { + fn to_p2wsh(&self) -> Result { self.wscript_hash().map(ScriptBuf::new_p2wsh) } /// 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: &Self, + &self, secp: &Secp256k1, internal_key: UntweakedPublicKey, ) -> ScriptBuf { @@ -59,7 +59,7 @@ define_extension_trait! { } /// Computes the P2SH output corresponding to this redeem script. - fn to_p2sh(self: &Self) -> Result { + fn to_p2sh(&self) -> Result { self.script_hash().map(ScriptBuf::new_p2sh) } @@ -67,7 +67,7 @@ define_extension_trait! { /// for a P2WPKH output. The `scriptCode` is described in [BIP143]. /// /// [BIP143]: - fn p2wpkh_script_code(self: &Self) -> Option { + fn p2wpkh_script_code(&self) -> Option { if self.is_p2wpkh() { // The `self` script is 0x00, 0x14, let bytes = &self.as_bytes()[2..]; @@ -82,14 +82,14 @@ define_extension_trait! { /// /// You can obtain the public key, if its valid, /// by calling [`p2pk_public_key()`](Self::p2pk_public_key) - fn is_p2pk(self: &Self) -> bool { self.p2pk_pubkey_bytes().is_some() } + fn is_p2pk(&self) -> bool { self.p2pk_pubkey_bytes().is_some() } /// Returns the public key if this script is P2PK with a **valid** public key. /// /// This may return `None` even when [`is_p2pk()`](Self::is_p2pk) returns true. /// This happens when the public key is invalid (e.g. the point not being on the curve). /// In this situation the script is unspendable. - fn p2pk_public_key(self: &Self) -> Option { + fn p2pk_public_key(&self) -> Option { PublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok() } } @@ -98,7 +98,7 @@ define_extension_trait! { define_extension_trait! { pub(crate) trait ScriptExtPrivate impl for Script { /// Returns the bytes of the (possibly invalid) public key if this script is P2PK. - fn p2pk_pubkey_bytes(self: &Self) -> Option<&[u8]> { + fn p2pk_pubkey_bytes(&self) -> Option<&[u8]> { match self.len() { 67 if self.as_bytes()[0] == OP_PUSHBYTES_65.to_u8() && self.as_bytes()[66] == OP_CHECKSIG.to_u8() => diff --git a/bitcoin/src/consensus_validation.rs b/bitcoin/src/consensus_validation.rs index f197a188e..b0177ec1e 100644 --- a/bitcoin/src/consensus_validation.rs +++ b/bitcoin/src/consensus_validation.rs @@ -131,7 +131,7 @@ define_extension_trait! { /// /// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html fn verify( - self: &Self, + &self, index: usize, amount: crate::Amount, spending_tx: &[u8], @@ -150,7 +150,7 @@ define_extension_trait! { /// /// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html fn verify_with_flags( - self: &Self, + &self, index: usize, amount: crate::Amount, spending_tx: &[u8], diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs index 32cc7e766..8dbeab0fd 100644 --- a/bitcoin/src/internal_macros.rs +++ b/bitcoin/src/internal_macros.rs @@ -251,7 +251,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),*>)?($($params:tt)*) $( -> $ret:ty )? $body:block )* }) => { $(#[$($trait_attrs)*])* $trait_vis trait $trait_name { @@ -259,7 +259,7 @@ macro_rules! define_extension_trait { $crate::internal_macros::only_doc_attrs! { { $(#[$($fn_attrs)*])* }, { - fn $fn$(<$($gen: $gent),*>)?($($param_name: $param_type),*) $( -> $ret )?; + fn $fn$(<$($gen: $gent),*>)?($($params)*) $( -> $ret )?; } } )* @@ -270,7 +270,7 @@ macro_rules! define_extension_trait { $crate::internal_macros::only_non_doc_attrs! { { $(#[$($fn_attrs)*])* }, { - fn $fn$(<$($gen: $gent),*>)?($($param_name: $param_type),*) $( -> $ret )? $body + fn $fn$(<$($gen: $gent),*>)?($($params)*) $( -> $ret )? $body } } )*