From 20156d1ac93120510fbff22dc23a00fb009b6b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Awirski?= Date: Thu, 17 Apr 2025 22:11:46 +0200 Subject: [PATCH] Cow without std --- miden-crypto/src/merkle/sparse_path.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/miden-crypto/src/merkle/sparse_path.rs b/miden-crypto/src/merkle/sparse_path.rs index c8f3b34..c8f9971 100644 --- a/miden-crypto/src/merkle/sparse_path.rs +++ b/miden-crypto/src/merkle/sparse_path.rs @@ -2,8 +2,8 @@ use alloc::vec::Vec; use core::{ iter::{self, FusedIterator}, num::NonZero, + ops::Deref, }; -use std::borrow::Cow; use winter_utils::{Deserializable, DeserializationError, Serializable}; @@ -253,10 +253,26 @@ impl From for Vec { // 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`]. pub struct SparseMerklePathIter<'p> { /// 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 /// exhausted. @@ -304,7 +320,7 @@ impl IntoIterator for SparseMerklePath { fn into_iter(self) -> SparseMerklePathIter<'static> { let tree_depth = self.depth(); SparseMerklePathIter { - path: Cow::Owned(self), + path: Inner::Owned(self), next_depth: tree_depth, } } @@ -317,7 +333,7 @@ impl<'p> IntoIterator for &'p SparseMerklePath { fn into_iter(self) -> SparseMerklePathIter<'p> { let tree_depth = self.depth(); SparseMerklePathIter { - path: Cow::Borrowed(self), + path: Inner::Borrowed(self), next_depth: tree_depth, } }