Skip to content

Commit 7c977b5

Browse files
committed
ForEachKey: clean up Concrete impl, remove Semantic impl
The Concrete impl used a weird complier gitch `let pred = |x| pred(x)` to avoid an infinite recursion bug (which in turn triggered a compiler bug that they didn't fix even though I filed it a DAY AFTER IT WAS INTRODUCED ON NIGHTLY and instead they waited 3 months and released it on stable forcing me to backport a workaround to a gazillion versions of rust-miniscript). The bug was rust-lang/rust#110475 BTW, just to make sure this commit triggers another notification on that issue. Clippy doesn't like this. Clippy is obviously wrong but rather than having this stupid fight again just switch to a different idiom. Meanwhile, the Semantic impl of ForEachKey panics iff any keys are present, regardless of the closure passed to it. This is funny but completely useless and we should just remove it.
1 parent 8bc9311 commit 7c977b5

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

src/policy/concrete.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
662662
where
663663
Pk: 'a,
664664
{
665-
let mut pred = |x| pred(x);
665+
self.for_each_key_internal(&mut pred)
666+
}
667+
}
668+
669+
impl<Pk: MiniscriptKey> Policy<Pk> {
670+
fn for_each_key_internal<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
666671
match *self {
667672
Policy::Unsatisfiable | Policy::Trivial => true,
668673
Policy::Key(ref pk) => pred(pk),
@@ -673,14 +678,12 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
673678
| Policy::After(..)
674679
| Policy::Older(..) => true,
675680
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
676-
subs.iter().all(|sub| sub.for_each_key(&mut pred))
681+
subs.iter().all(|sub| sub.for_each_key_internal(pred))
677682
}
678-
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.for_each_key(&mut pred)),
683+
Policy::Or(ref subs) => subs.iter().all(|(_, sub)| sub.for_each_key_internal(pred)),
679684
}
680685
}
681-
}
682686

683-
impl<Pk: MiniscriptKey> Policy<Pk> {
684687
/// Convert a policy using one kind of public key to another
685688
/// type of public key
686689
///

src/policy/semantic.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use elements::{LockTime, Sequence};
1010

1111
use super::concrete::PolicyError;
1212
use super::ENTAILMENT_MAX_TERMINALS;
13-
use crate::{errstr, expression, AbsLockTime, Error, ForEachKey, MiniscriptKey, Translator};
13+
use crate::{errstr, expression, AbsLockTime, Error, MiniscriptKey, Translator};
1414

1515
/// Abstract policy which corresponds to the semantics of a Miniscript
1616
/// and which allows complex forms of analysis, e.g. filtering and
@@ -59,26 +59,6 @@ where
5959
}
6060
}
6161

62-
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
63-
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
64-
where
65-
Pk: 'a,
66-
{
67-
let mut pred = |x| pred(x);
68-
match *self {
69-
Policy::Unsatisfiable | Policy::Trivial => true,
70-
Policy::Key(ref _pkh) => todo!("Semantic Policy KeyHash must store Pk"),
71-
Policy::Sha256(..)
72-
| Policy::Hash256(..)
73-
| Policy::Ripemd160(..)
74-
| Policy::Hash160(..)
75-
| Policy::After(..)
76-
| Policy::Older(..) => true,
77-
Policy::Threshold(_, ref subs) => subs.iter().all(|sub| sub.for_each_key(&mut pred)),
78-
}
79-
}
80-
}
81-
8262
impl<Pk: MiniscriptKey> Policy<Pk> {
8363
/// Convert a policy using one kind of public key to another
8464
/// type of public key

0 commit comments

Comments
 (0)