Skip to content

Commit df3a85a

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 e813ad0 commit df3a85a

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};
@@ -652,6 +653,30 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
652653
}
653654
}
654655

656+
impl<'a, Pk: MiniscriptKey> TreeLike for &'a Policy<Pk> {
657+
fn as_node(&self) -> Tree<Self> {
658+
use Policy::*;
659+
660+
match *self {
661+
Unsatisfiable | Trivial | Key(_) | After(_) | Older(_) | Sha256(_) | Hash256(_)
662+
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
663+
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
664+
}
665+
}
666+
}
667+
668+
impl<'a, Pk: MiniscriptKey> TreeLike for Arc<Policy<Pk>> {
669+
fn as_node(&self) -> Tree<Self> {
670+
use Policy::*;
671+
672+
match self.as_ref() {
673+
Unsatisfiable | Trivial | Key(_) | After(_) | Older(_) | Sha256(_) | Hash256(_)
674+
| Ripemd160(_) | Hash160(_) => Tree::Nullary,
675+
Threshold(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
676+
}
677+
}
678+
}
679+
655680
#[cfg(test)]
656681
mod tests {
657682
use core::str::FromStr;

0 commit comments

Comments
 (0)