Skip to content

Commit d8f7721

Browse files
Refactor ForEach to contain only Pk.
This change allows us to allow to refactor `Terminal::PkH(Pk::Hash)` to `Terminal::PkH(Pk)`.
1 parent c80aa7c commit d8f7721

File tree

8 files changed

+13
-24
lines changed

8 files changed

+13
-24
lines changed

src/descriptor/bare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
331331
Pk: 'a,
332332
Pk::Hash: 'a,
333333
{
334-
pred(ForEach::Key(&self.pk))
334+
pred(ForEach(&self.pk))
335335
}
336336
}
337337

src/descriptor/segwitv0.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
444444
Pk: 'a,
445445
Pk::Hash: 'a,
446446
{
447-
pred(ForEach::Key(&self.pk))
447+
pred(ForEach(&self.pk))
448448
}
449449
}
450450

src/descriptor/sortedmulti.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for SortedMultiVec<Pk
117117
Pk: 'a,
118118
Pk::Hash: 'a,
119119
{
120-
self.pks.iter().all(|key| pred(ForEach::Key(key)))
120+
self.pks.iter().all(|key| pred(ForEach(key)))
121121
}
122122
}
123123

src/descriptor/tr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
587587
let script_keys_res = self
588588
.iter_scripts()
589589
.all(|(_d, ms)| ms.for_each_key(&mut pred));
590-
script_keys_res && pred(ForEach::Key(&self.internal_key))
590+
script_keys_res && pred(ForEach(&self.internal_key))
591591
}
592592
}
593593

@@ -702,9 +702,6 @@ mod tests {
702702
let desc = desc.replace(&[' ', '\n'][..], "");
703703
let tr = Tr::<String>::from_str(&desc).unwrap();
704704
// Note the last ac12 only has ac and fails the predicate
705-
assert!(!tr.for_each_key(|k| match k {
706-
ForEach::Key(k) => k.starts_with("acc"),
707-
ForEach::Hash(_h) => unreachable!(),
708-
}));
705+
assert!(!tr.for_each_key(|k| k.0.starts_with("acc")));
709706
}
710707
}

src/lib.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -458,21 +458,13 @@ where
458458
T: Translator<P, Q, E>;
459459
}
460460

461-
/// Either a key or a keyhash
462-
pub enum ForEach<'a, Pk: MiniscriptKey> {
463-
/// A key
464-
Key(&'a Pk),
465-
/// A keyhash
466-
Hash(&'a Pk::Hash),
467-
}
461+
/// Either a key or keyhash, but both contain Pk
462+
pub struct ForEach<'a, Pk: MiniscriptKey>(&'a Pk);
468463

469464
impl<'a, Pk: MiniscriptKey<Hash = Pk>> ForEach<'a, Pk> {
470465
/// Convenience method to avoid distinguishing between keys and hashes when these are the same type
471466
pub fn as_key(&self) -> &'a Pk {
472-
match *self {
473-
ForEach::Key(ref_key) => ref_key,
474-
ForEach::Hash(ref_key) => ref_key,
475-
}
467+
self.0
476468
}
477469
}
478470

src/miniscript/astelem.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
8585
Pk::Hash: 'a,
8686
{
8787
match *self {
88-
Terminal::PkK(ref p) => pred(ForEach::Key(p)),
89-
Terminal::PkH(ref p) => pred(ForEach::Hash(p)),
88+
Terminal::PkK(ref p) => pred(ForEach(p)),
89+
Terminal::PkH(ref p) => todo!("KeyHash should contain Pk"),
9090
Terminal::After(..)
9191
| Terminal::Older(..)
9292
| Terminal::Sha256(..)
@@ -117,7 +117,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
117117
}
118118
Terminal::Thresh(_, ref subs) => subs.iter().all(|sub| sub.real_for_each_key(pred)),
119119
Terminal::Multi(_, ref keys) | Terminal::MultiA(_, ref keys) => {
120-
keys.iter().all(|key| pred(ForEach::Key(key)))
120+
keys.iter().all(|key| pred(ForEach(key)))
121121
}
122122
}
123123
}

src/policy/concrete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
304304
{
305305
match *self {
306306
Policy::Unsatisfiable | Policy::Trivial => true,
307-
Policy::Key(ref pk) => pred(ForEach::Key(pk)),
307+
Policy::Key(ref pk) => pred(ForEach(pk)),
308308
Policy::Sha256(..)
309309
| Policy::Hash256(..)
310310
| Policy::Ripemd160(..)

src/policy/semantic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
6363
{
6464
match *self {
6565
Policy::Unsatisfiable | Policy::Trivial => true,
66-
Policy::KeyHash(ref pkh) => pred(ForEach::Hash(pkh)),
66+
Policy::KeyHash(ref pkh) => todo!("Semantic Policy KeyHash must store Pk"),
6767
Policy::Sha256(..)
6868
| Policy::Hash256(..)
6969
| Policy::Ripemd160(..)

0 commit comments

Comments
 (0)