Skip to content

Commit

Permalink
Added missing IntoIterator trait for FixedVector (#20)
Browse files Browse the repository at this point in the history
* Added missing IntoIterator trait for FixedVector

* Added Iterator Test
  • Loading branch information
ethDreamer authored Dec 11, 2023
1 parent 2c603ec commit e22576e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/fixed_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ impl<'a, T, N: Unsigned> IntoIterator for &'a FixedVector<T, N> {
}
}

impl<T, N: Unsigned> IntoIterator for FixedVector<T, N> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;

fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}

impl<T, N: Unsigned> tree_hash::TreeHash for FixedVector<T, N>
where
T: tree_hash::TreeHash,
Expand Down Expand Up @@ -396,6 +405,17 @@ mod test {
assert_eq!(fixed.get(4), None);
}

#[test]
fn iterator() {
let vec = vec![0, 2, 4, 6];
let fixed: FixedVector<u64, U4> = FixedVector::from(vec);

// test the reference version
assert_eq!((&fixed).into_iter().sum::<u64>(), 12);
// test the owned version
assert_eq!(fixed.into_iter().sum::<u64>(), 12);
}

#[test]
fn ssz_encode() {
let vec: FixedVector<u16, U2> = vec![0; 2].into();
Expand Down

0 comments on commit e22576e

Please sign in to comment.