Skip to content

Commit 37f3796

Browse files
committed
Use TreeLike to implement for_each_key
Remove recursive calls and use `TreeLike`'s post order iterator to implement `for_each_key` for the `concrete::Policy`. No additional unit tests required, this code path is already tested.
1 parent e6229bf commit 37f3796

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

src/policy/concrete.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -554,30 +554,18 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
554554

555555
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
556556
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool {
557-
self.real_for_each_key(&mut pred)
558-
}
559-
}
560-
561-
impl<Pk: MiniscriptKey> Policy<Pk> {
562-
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
563-
match *self {
564-
Policy::Unsatisfiable | Policy::Trivial => true,
565-
Policy::Key(ref pk) => pred(pk),
566-
Policy::Sha256(..)
567-
| Policy::Hash256(..)
568-
| Policy::Ripemd160(..)
569-
| Policy::Hash160(..)
570-
| Policy::After(..)
571-
| Policy::Older(..) => true,
572-
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
573-
subs.iter().all(|sub| sub.real_for_each_key(&mut *pred))
557+
for data in self.post_order_iter() {
558+
if let Policy::Key(ref pk) = data.node {
559+
if !pred(pk) {
560+
return false;
561+
}
574562
}
575-
Policy::Or(ref subs) => subs
576-
.iter()
577-
.all(|(_, sub)| sub.real_for_each_key(&mut *pred)),
578563
}
564+
true
579565
}
566+
}
580567

568+
impl<Pk: MiniscriptKey> Policy<Pk> {
581569
/// Converts a policy using one kind of public key to another type of public key.
582570
///
583571
/// For example usage please see [`crate::policy::semantic::Policy::translate_pk`].

0 commit comments

Comments
 (0)