Skip to content

Commit 9ddf516

Browse files
committed
Create macros for writing Translator functions
This is the first commit in a series of two in order to remove the PkTranslator trait. The triat has a general implementation of Tranlator like `impl Translator for PkTranslator where P::Sha256 ...`. However, this blanket implemented has constraints on associated types and makes it impossible to implement the trait for a generic type downstream. Rust compiler does not support for trait specialization yet, and so we should only provide a macro to ease implementation rather than a blanket implementation that causes duplicate conflicts downstream
1 parent f7531b5 commit 9ddf516

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/macros.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22
//!
33
//! Macros meant to be used inside the Rust Miniscript library
44
5+
/// Macro for failing translation for other associated types.
6+
/// Handy for testing String -> concrete keys as we don't want to specify these
7+
/// functions repeatedly.
8+
#[macro_export]
9+
macro_rules! translate_assoc_fail {
10+
($source: ty, $target:ty, $error_ty: ty) => {
11+
fn sha256(
12+
&mut self,
13+
_sha256: &<$source as MiniscriptKey>::Sha256,
14+
) -> Result<<$target as MiniscriptKey>::Sha256, $error_ty> {
15+
panic!("Called sha256 on translate_only_pk")
16+
}
17+
18+
fn hash256(
19+
&mut self,
20+
_hash256: &<$source as MiniscriptKey>::Hash256,
21+
) -> Result<<$target as MiniscriptKey>::Hash256, $error_ty> {
22+
panic!("Called hash256 on translate_only_pk")
23+
}
24+
};
25+
}
26+
27+
/// Macro for translation of associated types where the associated type is the same
28+
/// Handy for Derived -> concrete keys where the associated types are the same.
29+
#[macro_export]
30+
macro_rules! translate_assoc_clone {
31+
($source: ty, $target:ty, $error_ty: ty) => {
32+
fn sha256(
33+
&mut self,
34+
_sha256: &<$source as MiniscriptKey>::Sha256,
35+
) -> Result<<$target as MiniscriptKey>::Sha256, $error_ty> {
36+
Ok(sha256.clone())
37+
}
38+
39+
fn hash256(
40+
&mut self,
41+
_hash256: &<$source as MiniscriptKey>::Hash256,
42+
) -> Result<<$target as MiniscriptKey>::Hash256, $error_ty> {
43+
Ok(hash256.clone())
44+
}
45+
};
46+
}
547
/// Allows tests to create a miniscript directly from string as
648
/// `ms_str!("c:or_i(pk({}),pk({}))", pk1, pk2)`
749
#[cfg(test)]

0 commit comments

Comments
 (0)