Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing IntoIterator trait for FixedVector #20

Merged
merged 2 commits into from
Dec 11, 2023
Merged
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
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
Loading