Cow without std
This commit is contained in:
parent
866a6bd78a
commit
20156d1ac9
1 changed files with 20 additions and 4 deletions
|
@ -2,8 +2,8 @@ use alloc::vec::Vec;
|
||||||
use core::{
|
use core::{
|
||||||
iter::{self, FusedIterator},
|
iter::{self, FusedIterator},
|
||||||
num::NonZero,
|
num::NonZero,
|
||||||
|
ops::Deref,
|
||||||
};
|
};
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use winter_utils::{Deserializable, DeserializationError, Serializable};
|
use winter_utils::{Deserializable, DeserializationError, Serializable};
|
||||||
|
|
||||||
|
@ -253,10 +253,26 @@ impl From<SparseMerklePath> for Vec<RpoDigest> {
|
||||||
// ITERATORS
|
// ITERATORS
|
||||||
// ================================================================================================
|
// ================================================================================================
|
||||||
|
|
||||||
|
enum Inner<'a, T> {
|
||||||
|
Owned(T),
|
||||||
|
Borrowed(&'a T),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> Deref for Inner<'a, T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
match *self {
|
||||||
|
Self::Borrowed(borrowed) => borrowed,
|
||||||
|
Self::Owned(ref owned) => &owned,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Iterator for [`SparseMerklePath`].
|
/// Iterator for [`SparseMerklePath`].
|
||||||
pub struct SparseMerklePathIter<'p> {
|
pub struct SparseMerklePathIter<'p> {
|
||||||
/// The "inner" value we're iterating over.
|
/// The "inner" value we're iterating over.
|
||||||
path: Cow<'p, SparseMerklePath>,
|
path: Inner<'p, SparseMerklePath>,
|
||||||
|
|
||||||
/// The depth a `next()` call will get. `next_depth == 0` indicates that the iterator has been
|
/// The depth a `next()` call will get. `next_depth == 0` indicates that the iterator has been
|
||||||
/// exhausted.
|
/// exhausted.
|
||||||
|
@ -304,7 +320,7 @@ impl IntoIterator for SparseMerklePath {
|
||||||
fn into_iter(self) -> SparseMerklePathIter<'static> {
|
fn into_iter(self) -> SparseMerklePathIter<'static> {
|
||||||
let tree_depth = self.depth();
|
let tree_depth = self.depth();
|
||||||
SparseMerklePathIter {
|
SparseMerklePathIter {
|
||||||
path: Cow::Owned(self),
|
path: Inner::Owned(self),
|
||||||
next_depth: tree_depth,
|
next_depth: tree_depth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -317,7 +333,7 @@ impl<'p> IntoIterator for &'p SparseMerklePath {
|
||||||
fn into_iter(self) -> SparseMerklePathIter<'p> {
|
fn into_iter(self) -> SparseMerklePathIter<'p> {
|
||||||
let tree_depth = self.depth();
|
let tree_depth = self.depth();
|
||||||
SparseMerklePathIter {
|
SparseMerklePathIter {
|
||||||
path: Cow::Borrowed(self),
|
path: Inner::Borrowed(self),
|
||||||
next_depth: tree_depth,
|
next_depth: tree_depth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue