Skip to content

Commit 0c43f97

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 f80e819 commit 0c43f97

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/descriptor/tr/taptree.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for TapTree<Pk> {
137137
/// D E
138138
/// would yield (2, A), (2, B), (2,C), (3, D), (3, E).
139139
///
140-
#[derive(Debug, Clone)]
140+
#[derive(Debug, Clone, Default)]
141141
pub struct TapTreeIter<'tr, Pk: MiniscriptKey> {
142142
inner: core::slice::Iter<'tr, (u8, Arc<Miniscript<Pk, Tap>>)>,
143143
}
@@ -160,6 +160,20 @@ impl<'tr, Pk: MiniscriptKey> Iterator for TapTreeIter<'tr, Pk> {
160160
}
161161
}
162162

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

0 commit comments

Comments
 (0)