fn prune_witness_subtree<'a, I>(
    witness: &Witness,
    children: &mut Peekable<I>,
    curr_path: &mut Vec<Label>,
) -> Result<Witness, TreeHashError>
where
    I: Iterator<Item = (&'a Label, &'a LabeledTree<Vec<u8>>)>,
{
    match witness {
        Witness::Fork {
            left_tree,
            right_tree,
        } => {
            let left = prune_witness_subtree(left_tree, children, curr_path)?;
            let right = prune_witness_subtree(right_tree, children, curr_path)?;

            match (&left, &right) {
                // Both children got pruned, replace by a `Pruned` node.
                (
                    Witness::Pruned {
                        digest: left_digest,
                    },
                    Witness::Pruned {
                        digest: right_digest,
                    },
                ) => Ok(Witness::Pruned {
                    digest: compute_fork_digest(left_digest, right_digest),
                }),

                // Still have some (possibly modified) non-pruned nodes, create a `Fork`.
                _ => Ok(Witness::Fork {
                    left_tree: Box::new(left),
                    right_tree: Box::new(right),
                }),
            }
        }

        Witness::Node { label, sub_witness } => {
            match children.peek() {
                // Labeled branch that requires some pruning.
                Some(&(tree_label, child)) if tree_label == label => {
                    children.next();

                    curr_path.push(label.to_owned());
                    let res = prune_witness_impl(sub_witness, child, curr_path)?;
                    curr_path.pop();

                    if let Witness::Pruned { digest } = res {
                        // Child was pruned, prune the `Node`.
                        Ok(Witness::Pruned {
                            digest: compute_node_digest(label, &digest),
                        })
                    } else {
                        // Return `Node` with (possibly) modified child.
                        Ok(Witness::Node {
                            label: label.to_owned(),
                            sub_witness: Box::new(res),
                        })
                    }
                }

                // Labeled branch to be kept.
                _ => Ok(witness.to_owned()),
            }
        }

        // Already pruned `Node` or `Fork`, all done.
        Witness::Pruned { .. } => Ok(witness.to_owned()),

        Witness::Known() => Err(TreeHashError::InconsistentPartialTree {
            offending_path: curr_path.to_owned(),
        }),
    }
}
/// Prunes from `witness` the given (non-empty) `LabeledTree::SubTree` children.
///
/// A non-empty `LabeledTree::Subtree` maps to a tree of zero or more
/// `Witness::Forks` with `Witness::Node` or `Witness::Known` leaves, so subtree
/// pruning is recursive.
///
/// `children` is an iterator over the `SubTree` children to be pruned, sorted
/// by label. If one or more children could not be pruned (because no such nodes
/// exist in the witness) the iterator will not be consumed and will point to
/// the first such child.
Prunes from `witness` the given (non-empty) `LabeledTree::SubTree` children.

`children` is an iterator over the `SubTree` children to be pruned, sorted
by label. If one or more children could not be pruned (because no such nodes
exist in the witness) the iterator will not be consumed and will point to
the first such child.
Prunes from ` witness ` the given ( non - empty ) ` LabeledTree :: SubTree ` children . ` children ` is an iterator over the ` SubTree ` children to be pruned sorted by label . If one or more children could not be pruned ( because no such nodes exist in the witness ) the iterator will not be consumed and will point to the first such child .
fn prune_witness_subtree<'a, I>(
    witness: &Witness,
    children: &mut Peekable<I>,
    curr_path: &mut Vec<Label>,
) -> Result<Witness, TreeHashError>
where
    I: Iterator<Item = (&'a Label, &'a LabeledTree<Vec<u8>>)>,
{
    match witness {
        Witness::Fork {
            left_tree,
            right_tree,
        } => {
            let left = prune_witness_subtree(left_tree, children, curr_path)?;
            let right = prune_witness_subtree(right_tree, children, curr_path)?;
            match (&left, &right) {
                // Both children got pruned, replace by a `Pruned` node.
                (
                    Witness::Pruned {
                        digest: left_digest,
                    },
                    Witness::Pruned {
                        digest: right_digest,
                    },
                ) => Ok(Witness::Pruned {
                    digest: compute_fork_digest(left_digest, right_digest),
                }),
                // Still have some (possibly modified) non-pruned nodes, create a `Fork`.
                _ => Ok(Witness::Fork {
                    left_tree: Box::new(left),
                    right_tree: Box::new(right),
                }),
            }
        }
        Witness::Node { label, sub_witness } => {
            match children.peek() {
                // Labeled branch that requires some pruning.
                Some(&(tree_label, child)) if tree_label == label => {
                    children.next();
                    curr_path.push(label.to_owned());
                    let res = prune_witness_impl(sub_witness, child, curr_path)?;
                    curr_path.pop();
                    if let Witness::Pruned { digest } = res {
                        // Child was pruned, prune the `Node`.
                        Ok(Witness::Pruned {
                            digest: compute_node_digest(label, &digest),
                        })
                    } else {
                        // Return `Node` with (possibly) modified child.
                        Ok(Witness::Node {
                            label: label.to_owned(),
                            sub_witness: Box::new(res),
                        })
                    }
                }
                // Labeled branch to be kept.
                _ => Ok(witness.to_owned()),
            }
        }
        // Already pruned `Node` or `Fork`, all done.
        Witness::Pruned { .. } => Ok(witness.to_owned()),
        Witness::Known() => Err(TreeHashError::InconsistentPartialTree {
            offending_path: curr_path.to_owned(),
        }),
    }
}
fn prune_witness_subtree < ' a , I > ( witness : & Witness , children : & mut Peekable < I > , curr_path : & mut Vec < Label > , ) -> Result < Witness , TreeHashError > where I : Iterator < Item = ( & ' a Label , & ' a LabeledTree < Vec < u8 > > ) > , { match witness { Witness :: Fork { left_tree , right_tree , } => { let left = prune_witness_subtree ( left_tree , children , curr_path ) ? ; let right = prune_witness_subtree ( right_tree , children , curr_path ) ? ; match ( & left , & right ) { // Both children got pruned, replace by a `Pruned` node. ( Witness :: Pruned { digest : left_digest , } , Witness :: Pruned { digest : right_digest , } , ) => Ok ( Witness :: Pruned { digest : compute_fork_digest ( left_digest , right_digest ) , } ) , // Still have some (possibly modified) non-pruned nodes, create a `Fork`. _ => Ok ( Witness :: Fork { left_tree : Box :: new ( left ) , right_tree : Box :: new ( right ) , } ) , } } Witness :: Node { label , sub_witness } => { match children . peek ( ) { // Labeled branch that requires some pruning. Some ( & ( tree_label , child ) ) if tree_label == label => { children . next ( ) ; curr_path . push ( label . to_owned ( ) ) ; let res = prune_witness_impl ( sub_witness , child , curr_path ) ? ; curr_path . pop ( ) ; if let Witness :: Pruned { digest } = res { // Child was pruned, prune the `Node`. Ok ( Witness :: Pruned { digest : compute_node_digest ( label , & digest ) , } ) } else { // Return `Node` with (possibly) modified child. Ok ( Witness :: Node { label : label . to_owned ( ) , sub_witness : Box :: new ( res ) , } ) } } // Labeled branch to be kept. _ => Ok ( witness . to_owned ( ) ) , } } // Already pruned `Node` or `Fork`, all done. Witness :: Pruned { .. } => Ok ( witness . to_owned ( ) ) , Witness :: Known ( ) => Err ( TreeHashError :: InconsistentPartialTree { offending_path : curr_path . to_owned ( ) , } ) , } }
Prunes from `witness` the given (non-empty) `LabeledTree::SubTree` children.
Prunes from ` witness ` the given ( non - empty ) ` LabeledTree :: SubTree ` children .
witness
&Witness
children
&mut Peekable<I>
curr_path
&mut Vec<Label>
witness
&Witness
children
&mut Peekable<I>
curr_path
&mut Vec<Label>