Skip to content

Commit 97ba4e9

Browse files
committed
semantic: Remove recursion in for_each_key
Done as part of the effort to remove all the recursion crate wide. Use the `TreeLike` trait to iterate over policy nodes and remove the recursive call in `semantic::Policy::for_each_key`.
1 parent df3a85a commit 97ba4e9

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

src/policy/semantic.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,14 @@ where
6666

6767
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
6868
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool {
69-
self.real_for_each_key(&mut pred)
69+
self.pre_order_iter().all(|policy| match policy {
70+
Policy::Key(ref pk) => pred(pk),
71+
_ => true,
72+
})
7073
}
7174
}
7275

7376
impl<Pk: MiniscriptKey> Policy<Pk> {
74-
fn real_for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: &mut F) -> bool {
75-
match *self {
76-
Policy::Unsatisfiable | Policy::Trivial => true,
77-
Policy::Key(ref pk) => pred(pk),
78-
Policy::Sha256(..)
79-
| Policy::Hash256(..)
80-
| Policy::Ripemd160(..)
81-
| Policy::Hash160(..)
82-
| Policy::After(..)
83-
| Policy::Older(..) => true,
84-
Policy::Threshold(_, ref subs) => {
85-
subs.iter().all(|sub| sub.real_for_each_key(&mut *pred))
86-
}
87-
}
88-
}
89-
9077
/// Converts a policy using one kind of public key to another type of public key.
9178
///
9279
/// # Examples

0 commit comments

Comments
 (0)