Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IntoIterator<Item = &Pk> for various types #252

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
}
}

impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Bare<Pk> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
self.ms.into_iter()
}
}

impl<Pk: MiniscriptKey> fmt::Debug for Bare<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.ms)
Expand Down Expand Up @@ -227,6 +236,15 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
}
}

impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Pkh<Pk> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
Box::new(::std::iter::once(ForEach::Key(&self.pk)))
}
}

impl<Pk: MiniscriptKey> fmt::Debug for Pkh<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "pkh({:?})", self.pk)
Expand Down
15 changes: 15 additions & 0 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,21 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
}
}

impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Descriptor<Pk> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
match *self {
Descriptor::Bare(ref bare) => bare.into_iter(),
Descriptor::Pkh(ref pk) => pk.into_iter(),
Descriptor::Wpkh(ref pk) => pk.into_iter(),
Descriptor::Sh(ref sh) => sh.into_iter(),
Descriptor::Wsh(ref wsh) => wsh.into_iter(),
}
}
}

impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
type Output = Descriptor<Q>;
/// Convert a descriptor using abstract keys to one using specific keys
Expand Down
21 changes: 21 additions & 0 deletions src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {
}
}

impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Wsh<Pk> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
match self.inner {
WshInner::SortedMulti(ref sm) => sm.into_iter(),
WshInner::Ms(ref ms) => ms.into_iter(),
}
}
}

/// Wsh Inner
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum WshInner<Pk: MiniscriptKey> {
Expand Down Expand Up @@ -315,6 +327,15 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
}
}

impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Wpkh<Pk> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
Box::new(::std::iter::once(ForEach::Key(&self.pk)))
}
}

impl<Pk: MiniscriptKey> fmt::Debug for Wpkh<Pk> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "wpkh({:?})", self.pk)
Expand Down
14 changes: 14 additions & 0 deletions src/descriptor/sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
}
}

impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Sh<Pk> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
match self.inner {
ShInner::Wsh(ref wsh) => wsh.into_iter(),
ShInner::Wpkh(ref wpkh) => wpkh.into_iter(),
ShInner::SortedMulti(ref smv) => smv.into_iter(),
ShInner::Ms(ref ms) => ms.into_iter(),
}
}
}

impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
fn sanity_check(&self) -> Result<(), Error> {
match self.inner {
Expand Down
9 changes: 9 additions & 0 deletions src/descriptor/sortedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
}
}

impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> IntoIterator for &'a SortedMultiVec<Pk, Ctx> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
Box::new(self.pks.iter().map(|pk| ForEach::Key(pk)))
}
}

impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for SortedMultiVec<Pk, Ctx> {
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
where
Expand Down
42 changes: 42 additions & 0 deletions src/miniscript/astelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,45 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
}
}
}

impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> IntoIterator for &'a Terminal<Pk, Ctx> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
use std::iter;

match self {
Terminal::PkK(ref pk) => Box::new(iter::once(ForEach::Key(pk))),
Terminal::PkH(ref pkh) => Box::new(iter::once(ForEach::Hash(pkh))),
Terminal::True
| Terminal::False
| Terminal::After(_)
| Terminal::Older(_)
| Terminal::Sha256(_)
| Terminal::Hash256(_)
| Terminal::Ripemd160(_)
| Terminal::Hash160(_) => Box::new(iter::empty()),
Terminal::Alt(ref i)
| Terminal::Swap(ref i)
| Terminal::Check(ref i)
| Terminal::DupIf(ref i)
| Terminal::Verify(ref i)
| Terminal::NonZero(ref i)
| Terminal::ZeroNotEqual(ref i) => i.into_iter(),
Terminal::AndV(ref i1, ref i2)
| Terminal::AndB(ref i1, ref i2)
| Terminal::OrB(ref i1, ref i2)
| Terminal::OrD(ref i1, ref i2)
| Terminal::OrC(ref i1, ref i2)
| Terminal::OrI(ref i1, ref i2) => Box::new(i1.into_iter().chain(i2.into_iter())),
Terminal::AndOr(ref i1, ref i2, ref i3) => {
Box::new(i1.into_iter().chain(i2.into_iter()).chain(i3.into_iter()))
}
Terminal::Thresh(_, ref scripts) => {
Box::new(scripts.iter().map(|s| s.into_iter()).flatten())
}
Terminal::Multi(_, ref pks) => Box::new(pks.iter().map(|pk| ForEach::Key(pk))),
}
}
}
9 changes: 9 additions & 0 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ where
}
}

impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> IntoIterator for &'a Miniscript<Pk, Ctx> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
self.node.into_iter()
}
}

serde_string_impl_pk!(Miniscript, "a miniscript", Ctx; ScriptContext);

#[cfg(test)]
Expand Down
25 changes: 25 additions & 0 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Policy<Pk> {
}
}

impl<'a, Pk: MiniscriptKey> IntoIterator for &'a Policy<Pk> {
type Item = ForEach<'a, Pk>;
type IntoIter = Box<dyn Iterator<Item = ForEach<'a, Pk>> + 'a>;

fn into_iter(self) -> Self::IntoIter {
use std::iter;

match *self {
Policy::Key(ref pk) => Box::new(iter::once(ForEach::Key(pk))),
Policy::Threshold(_, ref subs) | Policy::And(ref subs) => {
Box::new(subs.iter().map(|s| s.into_iter()).flatten())
}
Policy::Or(ref subs) => Box::new(subs.iter().map(|(_, s)| s.into_iter()).flatten()),
Policy::Unsatisfiable
| Policy::Trivial
| Policy::Sha256(..)
| Policy::Hash256(..)
| Policy::Ripemd160(..)
| Policy::Hash160(..)
| Policy::After(..)
| Policy::Older(..) => Box::new(iter::empty()),
}
}
}

impl<Pk: MiniscriptKey> Policy<Pk> {
/// Convert a policy using one kind of public key to another
/// type of public key
Expand Down