Skip to content

Commit bf02479

Browse files
committed
semantic: Remove recursion in real_*_timelocks
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 the `semantic::Policy::real_*_timelocks` functions. Done together because they are basically identical.
1 parent 8b122e5 commit bf02479

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

src/policy/semantic.rs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
488488

489489
/// Helper function to do the recursion in `timelocks`.
490490
fn real_relative_timelocks(&self) -> Vec<u32> {
491-
match *self {
492-
Policy::Unsatisfiable
493-
| Policy::Trivial
494-
| Policy::Key(..)
495-
| Policy::Sha256(..)
496-
| Policy::Hash256(..)
497-
| Policy::Ripemd160(..)
498-
| Policy::Hash160(..) => vec![],
499-
Policy::After(..) => vec![],
500-
Policy::Older(t) => vec![t.to_consensus_u32()],
501-
Policy::Threshold(_, ref subs) => subs.iter().fold(vec![], |mut acc, x| {
502-
acc.extend(x.real_relative_timelocks());
503-
acc
504-
}),
505-
}
491+
self.pre_order_iter()
492+
.filter_map(|policy| match policy {
493+
Policy::Older(t) => Some(t.to_consensus_u32()),
494+
_ => None,
495+
})
496+
.collect()
506497
}
507498

508499
/// Returns a list of all relative timelocks, not including 0, which appear
@@ -516,21 +507,12 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
516507

517508
/// Helper function for recursion in `absolute timelocks`
518509
fn real_absolute_timelocks(&self) -> Vec<u32> {
519-
match *self {
520-
Policy::Unsatisfiable
521-
| Policy::Trivial
522-
| Policy::Key(..)
523-
| Policy::Sha256(..)
524-
| Policy::Hash256(..)
525-
| Policy::Ripemd160(..)
526-
| Policy::Hash160(..) => vec![],
527-
Policy::Older(..) => vec![],
528-
Policy::After(t) => vec![t.to_u32()],
529-
Policy::Threshold(_, ref subs) => subs.iter().fold(vec![], |mut acc, x| {
530-
acc.extend(x.real_absolute_timelocks());
531-
acc
532-
}),
533-
}
510+
self.pre_order_iter()
511+
.filter_map(|policy| match policy {
512+
Policy::After(t) => Some(t.to_u32()),
513+
_ => None,
514+
})
515+
.collect()
534516
}
535517

536518
/// Returns a list of all absolute timelocks, not including 0, which appear

0 commit comments

Comments
 (0)