Skip to content

Commit c81389f

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 b3c0955 commit c81389f

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
@@ -544,30 +544,14 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
544544

545545
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
546546
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool {
547-
self.real_for_each_key(&mut pred)
547+
self.pre_order_iter().all(|policy| match policy {
548+
Policy::Key(ref pk) => pred(pk),
549+
_ => true,
550+
})
548551
}
549552
}
550553

551554
impl<Pk: MiniscriptKey> Policy<Pk> {
552-
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
553-
match *self {
554-
Policy::Unsatisfiable | Policy::Trivial => true,
555-
Policy::Key(ref pk) => pred(pk),
556-
Policy::Sha256(..)
557-
| Policy::Hash256(..)
558-
| Policy::Ripemd160(..)
559-
| Policy::Hash160(..)
560-
| Policy::After(..)
561-
| Policy::Older(..) => true,
562-
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
563-
subs.iter().all(|sub| sub.real_for_each_key(&mut *pred))
564-
}
565-
Policy::Or(ref subs) => subs
566-
.iter()
567-
.all(|(_, sub)| sub.real_for_each_key(&mut *pred)),
568-
}
569-
}
570-
571555
/// Converts a policy using one kind of public key to another type of public key.
572556
///
573557
/// For example usage please see [`crate::policy::semantic::Policy::translate_pk`].

0 commit comments

Comments
 (0)