Skip to content

Commit 25b3cb0

Browse files
committed
Implement TreeLike for semantic::Policy
In preparation for removing recursive algorithms in the `semantic` module implement the `TreeLike` trait to enable iteration over policy nodes. This is a direct copy of the `concrete::Policy` impl with the `And` and `Or` variants removed.
1 parent 9de17fc commit 25b3cb0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/policy/semantic.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use bitcoin::{absolute, Sequence};
1212

1313
use super::concrete::PolicyError;
1414
use super::ENTAILMENT_MAX_TERMINALS;
15+
use crate::iter::{Tree, TreeLike};
1516
use crate::prelude::*;
1617
use crate::sync::Arc;
1718
use crate::{errstr, expression, AbsLockTime, Error, ForEachKey, MiniscriptKey, Translator};
@@ -661,6 +662,30 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
661662
}
662663
}
663664

665+
impl<'a, Pk: MiniscriptKey> TreeLike for &'a Policy<Pk> {
666+
fn as_node(&self) -> Tree<Self> {
667+
use Policy::*;
668+
669+
match *self {
670+
Unsatisfiable | Trivial | Key(_) | After(_) | Older(_) | Sha256(_) | Hash256(_)
671+
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
672+
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
673+
}
674+
}
675+
}
676+
677+
impl<'a, Pk: MiniscriptKey> TreeLike for Arc<Policy<Pk>> {
678+
fn as_node(&self) -> Tree<Self> {
679+
use Policy::*;
680+
681+
match self.as_ref() {
682+
Unsatisfiable | Trivial | Key(_) | After(_) | Older(_) | Sha256(_) | Hash256(_)
683+
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
684+
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
685+
}
686+
}
687+
}
688+
664689
#[cfg(test)]
665690
mod tests {
666691
use core::str::FromStr;

0 commit comments

Comments
 (0)