Skip to content

Commit e1f0802

Browse files
committed
macros: make target pk and error type optional for translate_* macros
There is no reason callers need to provide these types explicitly. Allow them to do so anyway for backward compatibility. (This commit I pulled out of my mega-refactor branch, but it seemed useful here, so I'm pulling it in now.)
1 parent 77ed78b commit e1f0802

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

src/descriptor/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ impl Descriptor<DescriptorPublicKey> {
674674
pk.clone().at_derivation_index(self.0)
675675
}
676676

677-
translate_hash_clone!(DescriptorPublicKey, DescriptorPublicKey, ConversionError);
677+
translate_hash_clone!(DescriptorPublicKey);
678678
}
679679
self.translate_pk(&mut Derivator(index))
680680
.map_err(|e| e.expect_translator_err("No Context errors while translating"))
@@ -918,7 +918,7 @@ impl Descriptor<DescriptorPublicKey> {
918918
.ok_or(Error::MultipathDescLenMismatch),
919919
}
920920
}
921-
translate_hash_clone!(DescriptorPublicKey, DescriptorPublicKey, Error);
921+
translate_hash_clone!(DescriptorPublicKey);
922922
}
923923

924924
for (i, desc) in descriptors.iter_mut().enumerate() {
@@ -972,7 +972,7 @@ impl Descriptor<DefiniteDescriptorKey> {
972972
pk.derive_public_key(self.0)
973973
}
974974

975-
translate_hash_clone!(DefiniteDescriptorKey, bitcoin::PublicKey, ConversionError);
975+
translate_hash_clone!(DefiniteDescriptorKey);
976976
}
977977

978978
let derived = self.translate_pk(&mut Derivator(secp));

src/interpreter/inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::PublicKey, Ctx> {
364364
Ok(BitcoinKey::Fullkey(*pk))
365365
}
366366

367-
translate_hash_clone!(bitcoin::PublicKey, BitcoinKey, Self::Error);
367+
translate_hash_clone!(bitcoin::PublicKey);
368368
}
369369

370370
self.translate_pk_ctx(&mut TranslateFullPk)
@@ -384,7 +384,7 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::key::XOnlyPublicKey,
384384
Ok(BitcoinKey::XOnlyPublicKey(*pk))
385385
}
386386

387-
translate_hash_clone!(bitcoin::key::XOnlyPublicKey, BitcoinKey, Self::Error);
387+
translate_hash_clone!(bitcoin::key::XOnlyPublicKey);
388388
}
389389
self.translate_pk_ctx(&mut TranslateXOnlyPk)
390390
.expect("Translation should succeed")

src/policy/semantic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
8989
///
9090
/// // Handy macro for failing if we encounter any other fragment.
9191
/// // See also [`translate_hash_clone!`] for cloning instead of failing.
92-
/// translate_hash_fail!(String, bitcoin::PublicKey, ());
92+
/// translate_hash_fail!(String);
9393
/// }
9494
///
9595
/// let mut pk_map = HashMap::new();

src/psbt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ impl Translator<DefiniteDescriptorKey> for KeySourceLookUp {
999999
Ok(derived)
10001000
}
10011001

1002-
translate_hash_clone!(DescriptorPublicKey, bitcoin::PublicKey, descriptor::ConversionError);
1002+
translate_hash_clone!(DescriptorPublicKey);
10031003
}
10041004

10051005
// Provides generalized access to PSBT fields common to inputs and outputs

src/pub_macros.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
/// ```
4848
#[macro_export]
4949
macro_rules! translate_hash_fail {
50+
($source: ty) => {
51+
// The target and error type must always be these values; but we let
52+
// the user specify them explicitly for compatibility with the version
53+
// of this macro that existed before 13.x.
54+
translate_hash_fail!($source, Self::TargetPk, Self::Error);
55+
};
5056
($source: ty, $target:ty, $error_ty: ty) => {
5157
fn sha256(
5258
&mut self,
@@ -88,6 +94,12 @@ macro_rules! translate_hash_fail {
8894
/// See also [`crate::translate_hash_fail`]
8995
#[macro_export]
9096
macro_rules! translate_hash_clone {
97+
($source: ty) => {
98+
// The target and error type must always be these values; but we let
99+
// the user specify them explicitly for compatibility with the version
100+
// of this macro that existed before 13.x.
101+
translate_hash_clone!($source, Self::TargetPk, Self::Error);
102+
};
91103
($source: ty, $target:ty, $error_ty: ty) => {
92104
fn sha256(
93105
&mut self,

0 commit comments

Comments
 (0)