TapTree iterator implementation

This commit is contained in:
Dr Maxim Orlovsky 2022-03-23 12:46:10 +01:00
parent 1295008dc6
commit e27f8ff594
No known key found for this signature in database
GPG Key ID: FFC0250947E5C6F7
1 changed files with 41 additions and 1 deletions

View File

@ -13,6 +13,7 @@
// //
use prelude::*; use prelude::*;
use core;
use io; use io;
@ -25,7 +26,7 @@ use util::psbt::map::Map;
use util::psbt::raw; use util::psbt::raw;
use util::psbt::Error; use util::psbt::Error;
use util::taproot::TapLeafHash; use util::taproot::{LeafInfo, TapLeafHash};
use util::taproot::{NodeInfo, TaprootBuilder}; use util::taproot::{NodeInfo, TaprootBuilder};
@ -117,6 +118,45 @@ impl TapTree {
pub fn into_inner(self) -> TaprootBuilder { pub fn into_inner(self) -> TaprootBuilder {
self.0 self.0
} }
/// Returns iterator for a taproot script tree, operating in DFS order over leaf depth and
/// leaf script pairs.
pub fn iter(&self) -> TapTreeIter {
self.into_iter()
}
}
/// Iterator for a taproot script tree, operating in DFS order over leaf depth and
/// leaf script pairs.
pub struct TapTreeIter<'tree> {
leaf_iter: core::slice::Iter<'tree, LeafInfo>,
}
impl<'tree> Iterator for TapTreeIter<'tree> {
type Item = (u8, &'tree Script);
fn next(&mut self) -> Option<Self::Item> {
self.leaf_iter.next().map(|leaf_info| {
(leaf_info.merkle_branch.as_inner().len() as u8, &leaf_info.script)
})
}
}
impl<'tree> IntoIterator for &'tree TapTree {
type Item = (u8, &'tree Script);
type IntoIter = TapTreeIter<'tree>;
fn into_iter(self) -> Self::IntoIter {
match (self.0.branch().len(), self.0.branch().last()) {
(1, Some(Some(root))) => {
TapTreeIter {
leaf_iter: root.leaves.iter()
}
}
// This should be unreachable as we Taptree is already finalized
_ => unreachable!("non-finalized tree builder inside TapTree"),
}
}
} }
impl Output { impl Output {