Skip to content

Commit b25ec35

Browse files
committed
tr: improve TapTreeIter a bunch
TapTreeIter is now a wrapper around core::slice::Iter. So we can propagate several traits: ExactSizeIterator, DoubleEndedIterator and FusedIterator. Also Default. Annoyingly we cannot propagate TrustedLenIterator since that's nightly-only. core::slice::Iter also implements AsRef<&[T]> which is tempting, but we don't propagate that since it feels like it reveals too much about the internals of TapTreeIter.
1 parent 960a6cc commit b25ec35

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/descriptor/tr/taptree.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for TapTree<Pk> {
138138
/// D E
139139
/// would yield (2, A), (2, B), (2,C), (3, D), (3, E).
140140
///
141-
#[derive(Debug, Clone)]
141+
#[derive(Debug, Clone, Default)]
142142
pub struct TapTreeIter<'tr, Pk: MiniscriptKey> {
143143
inner: core::slice::Iter<'tr, (u8, Arc<Miniscript<Pk, Tap>>)>,
144144
}
@@ -161,6 +161,20 @@ impl<'tr, Pk: MiniscriptKey> Iterator for TapTreeIter<'tr, Pk> {
161161
}
162162
}
163163

164+
impl<'tr, Pk: MiniscriptKey> DoubleEndedIterator for TapTreeIter<'tr, Pk> {
165+
fn next_back(&mut self) -> Option<Self::Item> {
166+
self.inner
167+
.next_back()
168+
.map(|&(depth, ref node)| TapTreeIterItem { depth, node })
169+
}
170+
}
171+
172+
impl<'tr, Pk: MiniscriptKey> ExactSizeIterator for TapTreeIter<'tr, Pk> {
173+
fn len(&self) -> usize { self.inner.len() }
174+
}
175+
176+
impl<'tr, Pk: MiniscriptKey> core::iter::FusedIterator for TapTreeIter<'tr, Pk> {}
177+
164178
/// Iterator over all of the leaves of a Taproot tree.
165179
///
166180
/// If there is no tree (i.e. this is a keyspend-only Taproot descriptor)

0 commit comments

Comments
 (0)