Skip to content

Commit bee044f

Browse files
committed
Merge rust-bitcoin#3137: De-sugar self
34e8212 Replace &self with self: &Self (Tobin C. Harding) Pull request description: `foo(&self)` is syntax sugar for `foo(self: &Self)`. The `define_extension_trait` is currently large, ugly, and not that expressive. If we use `self: &Self` then the macro is greatly simplified. (Also consuming version `self: Self`) De-sugar only, no logic changes. ACKs for top commit: apoelstra: ACK 34e8212 successfully ran local tests; lol this looks so much better Kixunil: ACK 34e8212 Tree-SHA512: 7ec81bee99ede328d73a661c9e683a774ba14404ceb89ecb06765bedddf04dc9721672775b9ad3a9e3356d510c76681848f24ce4392a59d53216d23e6a27d9ad
2 parents c59b9e3 + 34e8212 commit bee044f

File tree

3 files changed

+11
-92
lines changed

3 files changed

+11
-92
lines changed

bitcoin/src/address/script_pubkey.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ define_extension_trait! {
2222
/// Extension functionality to add scriptPubkey support to the [`Builder`] type.
2323
pub trait BuilderExt impl for Builder {
2424
/// Adds instructions to push a public key onto the stack.
25-
fn push_key(self, key: PublicKey) -> Builder {
25+
fn push_key(self: Self, key: PublicKey) -> Builder {
2626
if key.compressed {
2727
self.push_slice(key.inner.serialize())
2828
} else {
@@ -31,7 +31,7 @@ define_extension_trait! {
3131
}
3232

3333
/// Adds instructions to push an XOnly public key onto the stack.
34-
fn push_x_only_key(self, x_only_key: XOnlyPublicKey) -> Builder {
34+
fn push_x_only_key(self: Self, x_only_key: XOnlyPublicKey) -> Builder {
3535
self.push_slice(x_only_key.serialize())
3636
}
3737
}
@@ -42,14 +42,14 @@ define_extension_trait! {
4242
pub trait ScriptExt impl for Script {
4343
/// Computes the P2WSH output corresponding to this witnessScript (aka the "witness redeem
4444
/// script").
45-
fn to_p2wsh(&self) -> Result<ScriptBuf, WitnessScriptSizeError> {
45+
fn to_p2wsh(self: &Self) -> Result<ScriptBuf, WitnessScriptSizeError> {
4646
self.wscript_hash().map(ScriptBuf::new_p2wsh)
4747
}
4848

4949
/// Computes P2TR output with a given internal key and a single script spending path equal to
5050
/// the current script, assuming that the script is a Tapscript.
5151
fn to_p2tr<C: Verification>(
52-
&self,
52+
self: &Self,
5353
secp: &Secp256k1<C>,
5454
internal_key: UntweakedPublicKey,
5555
) -> ScriptBuf {
@@ -59,15 +59,15 @@ define_extension_trait! {
5959
}
6060

6161
/// Computes the P2SH output corresponding to this redeem script.
62-
fn to_p2sh(&self) -> Result<ScriptBuf, RedeemScriptSizeError> {
62+
fn to_p2sh(self: &Self) -> Result<ScriptBuf, RedeemScriptSizeError> {
6363
self.script_hash().map(ScriptBuf::new_p2sh)
6464
}
6565

6666
/// Returns the script code used for spending a P2WPKH output if this script is a script pubkey
6767
/// for a P2WPKH output. The `scriptCode` is described in [BIP143].
6868
///
6969
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
70-
fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
70+
fn p2wpkh_script_code(self: &Self) -> Option<ScriptBuf> {
7171
if self.is_p2wpkh() {
7272
// The `self` script is 0x00, 0x14, <pubkey_hash>
7373
let bytes = &self.as_bytes()[2..];
@@ -82,14 +82,14 @@ define_extension_trait! {
8282
///
8383
/// You can obtain the public key, if its valid,
8484
/// by calling [`p2pk_public_key()`](Self::p2pk_public_key)
85-
fn is_p2pk(&self) -> bool { self.p2pk_pubkey_bytes().is_some() }
85+
fn is_p2pk(self: &Self) -> bool { self.p2pk_pubkey_bytes().is_some() }
8686

8787
/// Returns the public key if this script is P2PK with a **valid** public key.
8888
///
8989
/// This may return `None` even when [`is_p2pk()`](Self::is_p2pk) returns true.
9090
/// This happens when the public key is invalid (e.g. the point not being on the curve).
9191
/// In this situation the script is unspendable.
92-
fn p2pk_public_key(&self) -> Option<PublicKey> {
92+
fn p2pk_public_key(self: &Self) -> Option<PublicKey> {
9393
PublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok()
9494
}
9595
}
@@ -98,7 +98,7 @@ define_extension_trait! {
9898
define_extension_trait! {
9999
pub(crate) trait ScriptExtPrivate impl for Script {
100100
/// Returns the bytes of the (possibly invalid) public key if this script is P2PK.
101-
fn p2pk_pubkey_bytes(&self) -> Option<&[u8]> {
101+
fn p2pk_pubkey_bytes(self: &Self) -> Option<&[u8]> {
102102
match self.len() {
103103
67 if self.as_bytes()[0] == OP_PUSHBYTES_65.to_u8()
104104
&& self.as_bytes()[66] == OP_CHECKSIG.to_u8() =>

bitcoin/src/consensus_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ define_extension_trait! {
131131
///
132132
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
133133
fn verify(
134-
&self,
134+
self: &Self,
135135
index: usize,
136136
amount: crate::Amount,
137137
spending_tx: &[u8],
@@ -150,7 +150,7 @@ define_extension_trait! {
150150
///
151151
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
152152
fn verify_with_flags(
153-
&self,
153+
self: &Self,
154154
index: usize,
155155
amount: crate::Amount,
156156
spending_tx: &[u8],

bitcoin/src/internal_macros.rs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -216,67 +216,6 @@ pub(crate) use impl_asref_push_bytes;
216216

217217
/// Defines an trait `$trait_name` and implements it for `ty`, used to define extension traits.
218218
macro_rules! define_extension_trait {
219-
// With self, no generics.
220-
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
221-
$(
222-
$(#[$($fn_attrs:tt)*])*
223-
fn $fn:ident($slf:ident $(, $param_name:ident: $param_type:ty)* $(,)?) $( -> $ret:ty )? $body:block
224-
)*
225-
}) => {
226-
$(#[$($trait_attrs)*])* $trait_vis trait $trait_name {
227-
$(
228-
$(#[$($fn_attrs)*])*
229-
fn $fn($slf $(, $param_name: $param_type)*) $( -> $ret )?;
230-
)*
231-
}
232-
233-
impl $trait_name for $ty {
234-
$(
235-
fn $fn($slf $(, $param_name: $param_type)*) $( -> $ret )? $body
236-
)*
237-
}
238-
};
239-
// With &self, no generics.
240-
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
241-
$(
242-
$(#[$($fn_attrs:tt)*])*
243-
fn $fn:ident(&$slf:ident $(, $param_name:ident: $param_type:ty)* $(,)?) $( -> $ret:ty )? $body:block
244-
)*
245-
}) => {
246-
$(#[$($trait_attrs)*])* $trait_vis trait $trait_name {
247-
$(
248-
$(#[$($fn_attrs)*])*
249-
fn $fn(&$slf $(, $param_name: $param_type)*) $( -> $ret )?;
250-
)*
251-
}
252-
253-
impl $trait_name for $ty {
254-
$(
255-
fn $fn(&$slf $(, $param_name: $param_type)*) $( -> $ret )? $body
256-
)*
257-
}
258-
};
259-
// With &self, with generics.
260-
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
261-
$(
262-
$(#[$($fn_attrs:tt)*])*
263-
fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?(&$slf:ident $(, $param_name:ident: $param_type:ty)* $(,)?) $( -> $ret:ty )? $body:block
264-
)*
265-
}) => {
266-
$(#[$($trait_attrs)*])* $trait_vis trait $trait_name {
267-
$(
268-
$(#[$($fn_attrs)*])*
269-
fn $fn$(<$($gen: $gent),*>)?(&$slf $(, $param_name: $param_type)*) $( -> $ret )?;
270-
)*
271-
}
272-
273-
impl $trait_name for $ty {
274-
$(
275-
fn $fn$(<$($gen: $gent),*>)?(&$slf $(, $param_name: $param_type)*) $( -> $ret )? $body
276-
)*
277-
}
278-
};
279-
// No self, with generics.
280219
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
281220
$(
282221
$(#[$($fn_attrs:tt)*])*
@@ -296,25 +235,5 @@ macro_rules! define_extension_trait {
296235
)*
297236
}
298237
};
299-
// No self, with generic `<T: AsRef<PushBytes>>`
300-
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
301-
$(
302-
$(#[$($fn_attrs:tt)*])*
303-
fn $fn:ident<T: AsRef<PushBytes>>($($param_name:ident: $param_type:ty),* $(,)?) $( -> $ret:ty )? $body:block
304-
)*
305-
}) => {
306-
$(#[$($trait_attrs)*])* $trait_vis trait $trait_name {
307-
$(
308-
$(#[$($fn_attrs)*])*
309-
fn $fn<T: AsRef<PushBytes>>($($param_name: $param_type),*) $( -> $ret )?;
310-
)*
311-
}
312-
313-
impl $trait_name for $ty {
314-
$(
315-
fn $fn<T: AsRef<PushBytes>>($($param_name: $param_type),*) $( -> $ret )? $body
316-
)*
317-
}
318-
};
319238
}
320239
pub(crate) use define_extension_trait;

0 commit comments

Comments
 (0)