Skip to content

Commit a76d3e1

Browse files
committed
semantic: Remove recursion in n_keys
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::n_keys`.
1 parent f00da39 commit a76d3e1

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/policy/semantic.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
605605
/// Counts the number of public keys and keyhashes referenced in a policy.
606606
/// Duplicate keys will be double-counted.
607607
pub fn n_keys(&self) -> usize {
608-
match *self {
609-
Policy::Unsatisfiable | Policy::Trivial => 0,
610-
Policy::Key(..) => 1,
611-
Policy::After(..)
612-
| Policy::Older(..)
613-
| Policy::Sha256(..)
614-
| Policy::Hash256(..)
615-
| Policy::Ripemd160(..)
616-
| Policy::Hash160(..) => 0,
617-
Policy::Threshold(_, ref subs) => subs.iter().map(|sub| sub.n_keys()).sum::<usize>(),
618-
}
608+
self.pre_order_iter()
609+
.filter(|policy| match policy {
610+
Policy::Key(..) => true,
611+
_ => false,
612+
})
613+
.count()
619614
}
620615

621616
/// Counts the minimum number of public keys for which signatures could be

0 commit comments

Comments
 (0)