Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions data/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ impl<T: CloneState> CloneState for Box<[T]> {
}
}

impl<T: CloneState> CloneState for Vec<T> {
fn clone_state(&self) -> Self {
self.iter().map(CloneState::clone_state).collect()
}
}

#[cfg(test)]
mod tests {
use super::CloneState;
Expand Down
55 changes: 49 additions & 6 deletions data/src/components/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ impl<T, M: VectorMode> Default for Vector<T, M> {
}
}

impl<T: Clone, M: CloneVectorMode> CloneState for Vector<T, M> {
fn clone_state(&self) -> Self {
M::clone(self)
}
}

impl<T: Foldable<HashFold>> Foldable<HashFold> for Vector<T, Normal> {
fn fold(&self, builder: HashFold) -> Hash {
let mut node = builder.into_node_fold();
Expand Down Expand Up @@ -341,6 +335,12 @@ impl<T: Clone, M: CloneVectorMode> Clone for Vector<T, M> {
}
}

impl<T: CloneState, M: CloneVectorMode> CloneState for Vector<T, M> {
fn clone_state(&self) -> Self {
M::clone_state(self)
}
}

impl<T: Encode, M: EncodeVectorMode> Encode for Vector<T, M> {
fn encode<E: bincode::enc::Encoder>(
&self,
Expand Down Expand Up @@ -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<T: Clone>(this: &Vector<T, Self>) -> Vector<T, Self>;

/// Like [`Self::clone`] but uses `CloneState` instead.
fn clone_state<T: CloneState>(this: &Vector<T, Self>) -> Vector<T, Self>;
}

impl CloneVectorMode for Normal {
Expand All @@ -596,6 +599,12 @@ impl CloneVectorMode for Normal {
vector: this.vector.clone(),
}
}

fn clone_state<T: CloneState>(this: &Vector<T, Self>) -> Vector<T, Self> {
Vector {
vector: this.vector.clone_state(),
}
}
}

impl CloneVectorMode for Prove<'_> {
Expand All @@ -604,6 +613,12 @@ impl CloneVectorMode for Prove<'_> {
vector: this.vector.clone(),
}
}

fn clone_state<T: CloneState>(this: &Vector<T, Self>) -> Vector<T, Self> {
Vector {
vector: this.vector.clone_state(),
}
}
}

impl CloneVectorMode for Verify {
Expand All @@ -612,6 +627,12 @@ impl CloneVectorMode for Verify {
vector: this.vector.clone(),
}
}

fn clone_state<T: CloneState>(this: &Vector<T, Self>) -> Vector<T, Self> {
Vector {
vector: this.vector.clone_state(),
}
}
}

/// Mode types that implement this trait support encoding of the [`Vector`] component.
Expand Down Expand Up @@ -721,6 +742,18 @@ impl<T> ProveImpl<T> {
}
}

impl<T: CloneState> CloneState for ProveImpl<T> {
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<T> {
Expand All @@ -744,6 +777,16 @@ impl<T> VerifyImpl<T> {
}
}

impl<T: CloneState> CloneState for VerifyImpl<T> {
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;

Expand Down
19 changes: 19 additions & 0 deletions data/src/partial_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand All @@ -48,6 +50,15 @@ impl<T> DefinedEntry<T> {
}
}

impl<T: CloneState> CloneState for DefinedEntry<T> {
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<usize, T>` which provides a more
Expand Down Expand Up @@ -371,6 +382,14 @@ impl<T> From<Vec<T>> for PartialVec<T> {
}
}

impl<T: CloneState> CloneState for PartialVec<T> {
fn clone_state(&self) -> Self {
Self {
entries: self.entries.clone_state(),
}
}
}

/// Entry within a partial vector
#[derive(Debug, Clone)]
pub enum RangeEntry<'a, T> {
Expand Down
Loading