Skip to content

Commit 1ad88d1

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 560daa1 commit 1ad88d1

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

src/policy/concrete.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -554,30 +554,14 @@ 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)
557+
self.pre_order_iter().all(|policy| match policy {
558+
Policy::Key(ref pk) => pred(pk),
559+
_ => true,
560+
})
558561
}
559562
}
560563

561564
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))
574-
}
575-
Policy::Or(ref subs) => subs
576-
.iter()
577-
.all(|(_, sub)| sub.real_for_each_key(&mut *pred)),
578-
}
579-
}
580-
581565
/// Converts a policy using one kind of public key to another type of public key.
582566
///
583567
/// For example usage please see [`crate::policy::semantic::Policy::translate_pk`].

0 commit comments

Comments
 (0)