Skip to content

Commit

Permalink
merkle: update MerkleLeaves trait with removing lifetime and associat…
Browse files Browse the repository at this point in the history
…ed iterator type

Signed-off-by: Dr Maxim Orlovsky <[email protected]>
  • Loading branch information
dr-orlovsky committed Nov 19, 2024
1 parent 5036712 commit a045b27
Showing 1 changed file with 14 additions and 43 deletions.
57 changes: 14 additions & 43 deletions commit_verify/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use core::{iter, slice};
use std::collections::{btree_set, BTreeSet};
use std::collections::BTreeSet;
use std::ops::SubAssign;

use amplify::confinement::Confined;
Expand Down Expand Up @@ -202,77 +201,49 @@ impl MerkleHash {

pub trait MerkleLeaves {
type Leaf: CommitId<CommitmentId = MerkleHash>;
// TODO: Remove associated type
type LeafIter<'tmp>: ExactSizeIterator<Item = Self::Leaf>
where Self: 'tmp;

fn merkle_leaves(&self) -> Self::LeafIter<'_>;
fn merkle_leaves(&self) -> impl ExactSizeIterator<Item = &Self::Leaf>;
}

impl<T, const MIN: usize> MerkleLeaves for Confined<Vec<T>, MIN, { u8::MAX as usize }>
where T: CommitId<CommitmentId = MerkleHash> + Copy
where T: CommitId<CommitmentId = MerkleHash>
{
type Leaf = T;
type LeafIter<'tmp>
= iter::Copied<slice::Iter<'tmp, T>>
where Self: 'tmp;

fn merkle_leaves(&self) -> Self::LeafIter<'_> { self.iter().copied() }
fn merkle_leaves(&self) -> impl ExactSizeIterator<Item = &T> { self.iter() }
}

impl<T: Ord, const MIN: usize> MerkleLeaves for Confined<BTreeSet<T>, MIN, { u8::MAX as usize }>
where T: CommitId<CommitmentId = MerkleHash> + Copy
where T: CommitId<CommitmentId = MerkleHash>
{
type Leaf = T;
type LeafIter<'tmp>
= iter::Copied<btree_set::Iter<'tmp, T>>
where Self: 'tmp;

fn merkle_leaves(&self) -> Self::LeafIter<'_> { self.iter().copied() }
fn merkle_leaves(&self) -> impl ExactSizeIterator<Item = &T> { self.iter() }
}

impl<T, const MIN: usize> MerkleLeaves for Confined<Vec<T>, MIN, { u16::MAX as usize }>
where T: CommitId<CommitmentId = MerkleHash> + Copy
where T: CommitId<CommitmentId = MerkleHash>
{
type Leaf = T;
type LeafIter<'tmp>
= iter::Copied<slice::Iter<'tmp, T>>
where Self: 'tmp;

fn merkle_leaves(&self) -> Self::LeafIter<'_> { self.iter().copied() }
fn merkle_leaves(&self) -> impl ExactSizeIterator<Item = &T> { self.iter() }
}

impl<T: Ord, const MIN: usize> MerkleLeaves for Confined<BTreeSet<T>, MIN, { u16::MAX as usize }>
where T: CommitId<CommitmentId = MerkleHash> + Copy
where T: CommitId<CommitmentId = MerkleHash>
{
type Leaf = T;
type LeafIter<'tmp>
= iter::Copied<btree_set::Iter<'tmp, T>>
where Self: 'tmp;

fn merkle_leaves(&self) -> Self::LeafIter<'_> { self.iter().copied() }
fn merkle_leaves(&self) -> impl ExactSizeIterator<Item = &T> { self.iter() }
}

impl<T, const MIN: usize> MerkleLeaves for Confined<Vec<T>, MIN, { u32::MAX as usize }>
where T: CommitId<CommitmentId = MerkleHash> + Copy
where T: CommitId<CommitmentId = MerkleHash>
{
type Leaf = T;
type LeafIter<'tmp>
= iter::Copied<slice::Iter<'tmp, T>>
where Self: 'tmp;

fn merkle_leaves(&self) -> Self::LeafIter<'_> { self.iter().copied() }
fn merkle_leaves(&self) -> impl ExactSizeIterator<Item = &T> { self.iter() }
}

impl<T: Ord, const MIN: usize> MerkleLeaves for Confined<BTreeSet<T>, MIN, { u32::MAX as usize }>
where T: CommitId<CommitmentId = MerkleHash> + Copy
where T: CommitId<CommitmentId = MerkleHash>
{
type Leaf = T;
type LeafIter<'tmp>
= iter::Copied<btree_set::Iter<'tmp, T>>
where Self: 'tmp;

fn merkle_leaves(&self) -> Self::LeafIter<'_> { self.iter().copied() }
fn merkle_leaves(&self) -> impl ExactSizeIterator<Item = &T> { self.iter() }
}

/// Helper struct to track depth when working with Merkle blocks.
Expand Down

0 comments on commit a045b27

Please sign in to comment.