Trait miniscript::iter::TreeLike
source · pub trait TreeLike: Clone + Sized {
// Required method
fn as_node(&self) -> Tree<Self>;
// Provided methods
fn n_children(&self) -> usize { ... }
fn nth_child(&self, n: usize) -> Option<Self> { ... }
fn pre_order_iter(self) -> PreOrderIter<Self> ⓘ { ... }
fn verbose_pre_order_iter(self) -> VerbosePreOrderIter<Self> ⓘ { ... }
fn post_order_iter(self) -> PostOrderIter<Self> ⓘ { ... }
}
Expand description
A trait for any structure which has the shape of a Miniscript tree.
As a general rule, this should be implemented on references to nodes, rather than nodes themselves, because it provides algorithms that assume copying is cheap.
To implement this trait, you only need to implement the TreeLike::as_node
method, which will usually be very mechanical. Everything else is provided.
However, to avoid allocations, it may make sense to also implement
TreeLike::n_children
and TreeLike::nth_child
because the default
implementations will allocate vectors for n-ary nodes.
Required Methods§
Provided Methods§
sourcefn n_children(&self) -> usize
fn n_children(&self) -> usize
Accessor for the number of children this node has.
sourcefn nth_child(&self, n: usize) -> Option<Self>
fn nth_child(&self, n: usize) -> Option<Self>
Accessor for the nth child of the node, if a child with that index exists.
sourcefn pre_order_iter(self) -> PreOrderIter<Self> ⓘ
fn pre_order_iter(self) -> PreOrderIter<Self> ⓘ
Obtains an iterator of all the nodes rooted at the node, in pre-order.
sourcefn verbose_pre_order_iter(self) -> VerbosePreOrderIter<Self> ⓘ
fn verbose_pre_order_iter(self) -> VerbosePreOrderIter<Self> ⓘ
Obtains a verbose iterator of all the nodes rooted at the DAG, in pre-order.
See the documentation of VerbosePreOrderIter
for more information about what
this does. Essentially, if you find yourself using Self::pre_order_iter
and
then adding a stack to manually track which items and their children have been
yielded, you may be better off using this iterator instead.
sourcefn post_order_iter(self) -> PostOrderIter<Self> ⓘ
fn post_order_iter(self) -> PostOrderIter<Self> ⓘ
Obtains an iterator of all the nodes rooted at the DAG, in post order.
Each node is only yielded once, at the leftmost position that it appears in the DAG.