Skip to content

Eliminate a bunch of recursion #567

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

Merged
merged 5 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/interpreter/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::PublicKey, Ctx> {
translate_hash_clone!(bitcoin::PublicKey, BitcoinKey, ());
}

self.real_translate_pk(&mut TranslateFullPk)
self.translate_pk_ctx(&mut TranslateFullPk)
.expect("Translation should succeed")
}
}
Expand All @@ -397,7 +397,7 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::key::XOnlyPublicKey,

translate_hash_clone!(bitcoin::key::XOnlyPublicKey, BitcoinKey, ());
}
self.real_translate_pk(&mut TranslateXOnlyPk)
self.translate_pk_ctx(&mut TranslateXOnlyPk)
.expect("Translation should succeed")
}
}
Expand Down
93 changes: 93 additions & 0 deletions src/iter/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Written in 2023 by Andrew Poelstra <[email protected]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @apoelstra, I saw this line and wondered if you added it as a habit or if I have been annoying you by removing them? Or was this PR just before we discussed all that? Cheers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it predates your PR to remove these. I would've just mildlessly copied the line from other files.

I'm certainly not annoyed :).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet man, cheers.

// SPDX-License-Identifier: CC0-1.0

//! Abstract Tree Iteration
//!
//! This module provides functionality to treat Miniscript objects abstractly
//! as trees, iterating over them in various orders. The iterators in this
//! module can be used to avoid explicitly recursive algorithms.
//!

mod tree;

pub use tree::{
PostOrderIter, PostOrderIterItem, PreOrderIter, PreOrderIterItem, Tree, TreeLike,
VerbosePreOrderIter,
};

use crate::sync::Arc;
use crate::{Miniscript, MiniscriptKey, ScriptContext, Terminal};

impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> TreeLike for &'a Miniscript<Pk, Ctx> {
fn as_node(&self) -> Tree<Self> {
match self.node {
Terminal::PkK(..)
| Terminal::PkH(..)
| Terminal::RawPkH(..)
| Terminal::After(..)
| Terminal::Older(..)
| Terminal::Sha256(..)
| Terminal::Hash256(..)
| Terminal::Ripemd160(..)
| Terminal::Hash160(..)
| Terminal::True
| Terminal::False
| Terminal::Multi(..)
| Terminal::MultiA(..) => Tree::Nullary,
Terminal::Alt(ref sub)
| Terminal::Swap(ref sub)
| Terminal::Check(ref sub)
| Terminal::DupIf(ref sub)
| Terminal::Verify(ref sub)
| Terminal::NonZero(ref sub)
| Terminal::ZeroNotEqual(ref sub) => Tree::Unary(sub),
Terminal::AndV(ref left, ref right)
| Terminal::AndB(ref left, ref right)
| Terminal::OrB(ref left, ref right)
| Terminal::OrD(ref left, ref right)
| Terminal::OrC(ref left, ref right)
| Terminal::OrI(ref left, ref right) => Tree::Binary(left, right),
Terminal::AndOr(ref a, ref b, ref c) => Tree::Nary(Arc::from([a.as_ref(), b, c])),
Terminal::Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()),
}
}
}

impl<Pk: MiniscriptKey, Ctx: ScriptContext> TreeLike for Arc<Miniscript<Pk, Ctx>> {
fn as_node(&self) -> Tree<Self> {
match self.node {
Terminal::PkK(..)
| Terminal::PkH(..)
| Terminal::RawPkH(..)
| Terminal::After(..)
| Terminal::Older(..)
| Terminal::Sha256(..)
| Terminal::Hash256(..)
| Terminal::Ripemd160(..)
| Terminal::Hash160(..)
| Terminal::True
| Terminal::False
| Terminal::Multi(..)
| Terminal::MultiA(..) => Tree::Nullary,
Terminal::Alt(ref sub)
| Terminal::Swap(ref sub)
| Terminal::Check(ref sub)
| Terminal::DupIf(ref sub)
| Terminal::Verify(ref sub)
| Terminal::NonZero(ref sub)
| Terminal::ZeroNotEqual(ref sub) => Tree::Unary(Arc::clone(sub)),
Terminal::AndV(ref left, ref right)
| Terminal::AndB(ref left, ref right)
| Terminal::OrB(ref left, ref right)
| Terminal::OrD(ref left, ref right)
| Terminal::OrC(ref left, ref right)
| Terminal::OrI(ref left, ref right) => {
Tree::Binary(Arc::clone(left), Arc::clone(right))
}
Terminal::AndOr(ref a, ref b, ref c) => {
Tree::Nary(Arc::from([Arc::clone(a), Arc::clone(b), Arc::clone(c)]))
}
Terminal::Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()),
}
}
}
Loading