use Cow instead of the additional IntoIter struct
This commit is contained in:
parent
10bc59c138
commit
0fb731753e
1 changed files with 18 additions and 53 deletions
|
@ -1,4 +1,4 @@
|
|||
use alloc::vec::Vec;
|
||||
use alloc::{borrow::Cow, vec::Vec};
|
||||
use core::{
|
||||
iter::{self, FusedIterator},
|
||||
num::NonZero,
|
||||
|
@ -252,20 +252,10 @@ impl From<SparseMerklePath> for Vec<RpoDigest> {
|
|||
// ITERATORS
|
||||
// ================================================================================================
|
||||
|
||||
impl<'p> IntoIterator for &'p SparseMerklePath {
|
||||
type Item = <SparseMerklePathIter<'p> as Iterator>::Item;
|
||||
type IntoIter = SparseMerklePathIter<'p>;
|
||||
|
||||
fn into_iter(self) -> SparseMerklePathIter<'p> {
|
||||
let tree_depth = self.depth();
|
||||
SparseMerklePathIter { path: self, next_depth: tree_depth }
|
||||
}
|
||||
}
|
||||
|
||||
/// Borrowing iterator for [`SparseMerklePath`].
|
||||
/// Iterator for [`SparseMerklePath`].
|
||||
pub struct SparseMerklePathIter<'p> {
|
||||
/// The "inner" value we're iterating over.
|
||||
path: &'p SparseMerklePath,
|
||||
path: Cow<'p, SparseMerklePath>,
|
||||
|
||||
/// The depth a `next()` call will get. `next_depth == 0` indicates that the iterator has been
|
||||
/// exhausted.
|
||||
|
@ -306,56 +296,31 @@ impl FusedIterator for SparseMerklePathIter<'_> {}
|
|||
|
||||
// TODO: impl DoubleEndedIterator.
|
||||
|
||||
/// Owning iterator for [SparseMerklePath].
|
||||
pub struct IntoIter {
|
||||
/// The "inner" value we're iterating over.
|
||||
path: SparseMerklePath,
|
||||
|
||||
/// The depth a `next()` call will get. `next_depth == 0` indicates that the iterator has been
|
||||
/// exhausted.
|
||||
next_depth: u8,
|
||||
}
|
||||
|
||||
impl IntoIterator for SparseMerklePath {
|
||||
type IntoIter = IntoIter;
|
||||
type IntoIter = SparseMerklePathIter<'static>;
|
||||
type Item = <Self::IntoIter as Iterator>::Item;
|
||||
|
||||
fn into_iter(self) -> IntoIter {
|
||||
fn into_iter(self) -> SparseMerklePathIter<'static> {
|
||||
let tree_depth = self.depth();
|
||||
IntoIter { path: self, next_depth: tree_depth }
|
||||
SparseMerklePathIter {
|
||||
path: Cow::Owned(self),
|
||||
next_depth: tree_depth,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for IntoIter {
|
||||
type Item = RpoDigest;
|
||||
impl<'p> IntoIterator for &'p SparseMerklePath {
|
||||
type Item = <SparseMerklePathIter<'p> as Iterator>::Item;
|
||||
type IntoIter = SparseMerklePathIter<'p>;
|
||||
|
||||
fn next(&mut self) -> Option<RpoDigest> {
|
||||
let this_depth = self.next_depth;
|
||||
// Paths don't include the root, so if `this_depth` is 0 then we keep returning `None`.
|
||||
let this_depth = NonZero::new(this_depth)?;
|
||||
self.next_depth = this_depth.get() - 1;
|
||||
|
||||
// `this_depth` is only ever decreasing, so it can't ever exceed `self.path.depth()`.
|
||||
let node = self.path.at_depth(this_depth).unwrap();
|
||||
Some(node)
|
||||
}
|
||||
|
||||
// IntoIter always knows its exact size.
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let remaining = ExactSizeIterator::len(self);
|
||||
(remaining, Some(remaining))
|
||||
fn into_iter(self) -> SparseMerklePathIter<'p> {
|
||||
let tree_depth = self.depth();
|
||||
SparseMerklePathIter {
|
||||
path: Cow::Borrowed(self),
|
||||
next_depth: tree_depth,
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for IntoIter {
|
||||
fn len(&self) -> usize {
|
||||
self.next_depth as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl FusedIterator for IntoIter {}
|
||||
|
||||
// TODO: impl DoubleEndedIterator.
|
||||
|
||||
// COMPARISONS
|
||||
// ================================================================================================
|
||||
|
|
Loading…
Add table
Reference in a new issue