Skip to content

Commit eb75602

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 d9f9d43 commit eb75602

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
@@ -596,17 +596,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
596596
/// Counts the number of public keys and keyhashes referenced in a policy.
597597
/// Duplicate keys will be double-counted.
598598
pub fn n_keys(&self) -> usize {
599-
match *self {
600-
Policy::Unsatisfiable | Policy::Trivial => 0,
601-
Policy::Key(..) => 1,
602-
Policy::After(..)
603-
| Policy::Older(..)
604-
| Policy::Sha256(..)
605-
| Policy::Hash256(..)
606-
| Policy::Ripemd160(..)
607-
| Policy::Hash160(..) => 0,
608-
Policy::Threshold(_, ref subs) => subs.iter().map(|sub| sub.n_keys()).sum::<usize>(),
609-
}
599+
self.pre_order_iter()
600+
.filter(|policy| match policy {
601+
Policy::Key(..) => true,
602+
_ => false,
603+
})
604+
.count()
610605
}
611606

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

0 commit comments

Comments
 (0)