Skip to content

Commit 8cf224c

Browse files
committed
Add IntoIterator<Item = &Pk> for Descriptor
1 parent 2c2f9c0 commit 8cf224c

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

src/descriptor/bare.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
6363
}
6464
}
6565

66+
impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Bare<Pk> {
67+
type Item = &'a Pk;
68+
type IntoIter = Box<dyn Iterator<Item = &'a Pk> + 'a>;
69+
70+
fn into_iter(self) -> Self::IntoIter {
71+
self.ms.into_iter()
72+
}
73+
}
74+
6675
impl<Pk: MiniscriptKey> fmt::Debug for Bare<Pk> {
6776
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6877
write!(f, "{:?}", self.ms)
@@ -227,6 +236,15 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
227236
}
228237
}
229238

239+
impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Pkh<Pk> {
240+
type Item = &'a Pk;
241+
type IntoIter = Box<dyn Iterator<Item = &'a Pk> + 'a>;
242+
243+
fn into_iter(self) -> Self::IntoIter {
244+
Box::new(::std::iter::once(&self.pk))
245+
}
246+
}
247+
230248
impl<Pk: MiniscriptKey> fmt::Debug for Pkh<Pk> {
231249
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
232250
write!(f, "pkh({:?})", self.pk)

src/descriptor/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
300300
}
301301
}
302302

303+
impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Descriptor<Pk> {
304+
type Item = &'a Pk;
305+
type IntoIter = Box<dyn Iterator<Item = &'a Pk> + 'a>;
306+
307+
fn into_iter(self) -> Self::IntoIter {
308+
match *self {
309+
Descriptor::Bare(ref bare) => bare.into_iter(),
310+
Descriptor::Pkh(ref pk) => pk.into_iter(),
311+
Descriptor::Wpkh(ref pk) => pk.into_iter(),
312+
Descriptor::Sh(ref sh) => sh.into_iter(),
313+
Descriptor::Wsh(ref wsh) => wsh.into_iter(),
314+
}
315+
}
316+
}
317+
303318
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
304319
type Output = Descriptor<Q>;
305320
/// Convert a descriptor using abstract keys to one using specific keys

src/descriptor/segwitv0.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {
7878
}
7979
}
8080

81+
impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Wsh<Pk> {
82+
type Item = &'a Pk;
83+
type IntoIter = Box<dyn Iterator<Item = &'a Pk> + 'a>;
84+
85+
fn into_iter(self) -> Self::IntoIter {
86+
match self.inner {
87+
WshInner::SortedMulti(ref sm) => sm.into_iter(),
88+
WshInner::Ms(ref ms) => ms.into_iter(),
89+
}
90+
}
91+
}
92+
8193
/// Wsh Inner
8294
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
8395
pub enum WshInner<Pk: MiniscriptKey> {
@@ -315,6 +327,15 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
315327
}
316328
}
317329

330+
impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Wpkh<Pk> {
331+
type Item = &'a Pk;
332+
type IntoIter = Box<dyn Iterator<Item = &'a Pk> + 'a>;
333+
334+
fn into_iter(self) -> Self::IntoIter {
335+
Box::new(::std::iter::once(&self.pk))
336+
}
337+
}
338+
318339
impl<Pk: MiniscriptKey> fmt::Debug for Wpkh<Pk> {
319340
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
320341
write!(f, "wpkh({:?})", self.pk)

src/descriptor/sh.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
193193
}
194194
}
195195

196+
impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Sh<Pk> {
197+
type Item = &'a Pk;
198+
type IntoIter = Box<dyn Iterator<Item = &'a Pk> + 'a>;
199+
200+
fn into_iter(self) -> Self::IntoIter {
201+
match self.inner {
202+
ShInner::Wsh(ref wsh) => wsh.into_iter(),
203+
ShInner::Wpkh(ref wpkh) => wpkh.into_iter(),
204+
ShInner::SortedMulti(ref smv) => smv.into_iter(),
205+
ShInner::Ms(ref ms) => ms.into_iter(),
206+
}
207+
}
208+
}
209+
196210
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
197211
fn sanity_check(&self) -> Result<(), Error> {
198212
match self.inner {

src/descriptor/sortedmulti.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
106106
}
107107
}
108108

109+
impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> IntoIterator for &'a SortedMultiVec<Pk, Ctx> {
110+
type Item = &'a Pk;
111+
type IntoIter = Box<dyn Iterator<Item = &'a Pk> + 'a>;
112+
113+
fn into_iter(self) -> Self::IntoIter {
114+
Box::new(self.pks.iter())
115+
}
116+
}
117+
109118
impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for SortedMultiVec<Pk, Ctx> {
110119
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
111120
where

0 commit comments

Comments
 (0)