From 7e548e68888063fbe89053d1eb42a7a6d1ffd048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Fri, 27 Mar 2026 15:49:54 +0000 Subject: [PATCH] feat(vector): add CloneState implementation --- data/src/clone.rs | 6 ++++ data/src/components/vector.rs | 55 +++++++++++++++++++++++++++++++---- data/src/partial_vec.rs | 19 ++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/data/src/clone.rs b/data/src/clone.rs index 7c588f56b09..f1c5072d1d5 100644 --- a/data/src/clone.rs +++ b/data/src/clone.rs @@ -51,6 +51,12 @@ impl CloneState for Box<[T]> { } } +impl CloneState for Vec { + fn clone_state(&self) -> Self { + self.iter().map(CloneState::clone_state).collect() + } +} + #[cfg(test)] mod tests { use super::CloneState; diff --git a/data/src/components/vector.rs b/data/src/components/vector.rs index a72cd5e18ef..2197c015dea 100644 --- a/data/src/components/vector.rs +++ b/data/src/components/vector.rs @@ -124,12 +124,6 @@ impl Default for Vector { } } -impl CloneState for Vector { - fn clone_state(&self) -> Self { - M::clone(self) - } -} - impl> Foldable for Vector { fn fold(&self, builder: HashFold) -> Hash { let mut node = builder.into_node_fold(); @@ -341,6 +335,12 @@ impl Clone for Vector { } } +impl CloneState for Vector { + fn clone_state(&self) -> Self { + M::clone_state(self) + } +} + impl Encode for Vector { fn encode( &self, @@ -588,6 +588,9 @@ pub trait CloneVectorMode: Mode { /// This clones the entire component, not just the internal value. Consider this when cloning /// components in [`crate::mode::Prove`] mode. fn clone(this: &Vector) -> Vector; + + /// Like [`Self::clone`] but uses `CloneState` instead. + fn clone_state(this: &Vector) -> Vector; } impl CloneVectorMode for Normal { @@ -596,6 +599,12 @@ impl CloneVectorMode for Normal { vector: this.vector.clone(), } } + + fn clone_state(this: &Vector) -> Vector { + Vector { + vector: this.vector.clone_state(), + } + } } impl CloneVectorMode for Prove<'_> { @@ -604,6 +613,12 @@ impl CloneVectorMode for Prove<'_> { vector: this.vector.clone(), } } + + fn clone_state(this: &Vector) -> Vector { + Vector { + vector: this.vector.clone_state(), + } + } } impl CloneVectorMode for Verify { @@ -612,6 +627,12 @@ impl CloneVectorMode for Verify { vector: this.vector.clone(), } } + + fn clone_state(this: &Vector) -> Vector { + Vector { + vector: this.vector.clone_state(), + } + } } /// Mode types that implement this trait support encoding of the [`Vector`] component. @@ -721,6 +742,18 @@ impl ProveImpl { } } +impl CloneState for ProveImpl { + fn clone_state(&self) -> Self { + Self { + previous: self.previous.clone_state(), + active_previous: self.active_previous, + accessed_indices: self.accessed_indices.clone(), + appended: self.appended.clone_state(), + read_length: self.read_length.clone(), + } + } +} + /// [`crate::mode::Verify`] mode implementation for the [`Vector`] component #[perfect_derive(Clone)] struct VerifyImpl { @@ -744,6 +777,16 @@ impl VerifyImpl { } } +impl CloneState for VerifyImpl { + fn clone_state(&self) -> Self { + Self { + original_length: self.original_length.clone(), + length: self.length.clone(), + items: self.items.clone_state(), + } + } +} + /// Arity of internal nodes in the Merkle tree const NODE_ARITY: usize = 4; diff --git a/data/src/partial_vec.rs b/data/src/partial_vec.rs index ebec5240db6..266a153b61a 100644 --- a/data/src/partial_vec.rs +++ b/data/src/partial_vec.rs @@ -31,6 +31,8 @@ use std::ops::Range; use perfect_derive::perfect_derive; +use crate::clone::CloneState; + /// Entry in the partial vector that represents a defined data range #[perfect_derive(Debug, Clone, Default)] struct DefinedEntry { @@ -48,6 +50,15 @@ impl DefinedEntry { } } +impl CloneState for DefinedEntry { + fn clone_state(&self) -> Self { + Self { + start: self.start, + data: self.data.clone_state(), + } + } +} + /// Partial vector that may not be defined for certain ranges /// /// This data type can be seen as an alternative to `BTreeMap` which provides a more @@ -371,6 +382,14 @@ impl From> for PartialVec { } } +impl CloneState for PartialVec { + fn clone_state(&self) -> Self { + Self { + entries: self.entries.clone_state(), + } + } +} + /// Entry within a partial vector #[derive(Debug, Clone)] pub enum RangeEntry<'a, T> {