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

feat(corelib): IntoIter for Span-convertible @C #6998

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,13 @@ impl SpanIntoIterator<T> of crate::iter::IntoIterator<Span<T>> {
}
}

impl SnapshotSpanIntoIterator<T> of crate::iter::IntoIterator<@Span<T>> {
type IntoIter = crate::array::SpanIter<T>;
fn into_iter(self: @Span<T>) -> Self::IntoIter {
(*self).into_iter()
}
}

/// An iterator struct over an array collection.
#[derive(Drop)]
pub struct ArrayIter<T> {
Expand Down Expand Up @@ -845,6 +852,13 @@ impl ArrayIntoIterator<T> of crate::iter::IntoIterator<Array<T>> {
}
}

impl SnapshotArrayIntoIterator<T> of crate::iter::IntoIterator<@Array<T>> {
type IntoIter = SpanIter<T>;
fn into_iter(self: @Array<T>) -> Self::IntoIter {
self.span().into_iter()
}
}

/// Information about a fixed-sized array.
trait FixedSizedArrayInfo<S> {
/// The type of the elements in the array.
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/iter/traits/collect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl IteratorIntoIterator<T, +Iterator<T>> of IntoIterator<T> {
}

impl SnapshotFixedSizeArrayIntoIterator<
T, const SIZE: usize, +Drop<T>, impl ToSpan: core::array::ToSpanTrait<[T; SIZE], T>,
T, const SIZE: usize, +Drop<T>, impl ToSpan: crate::array::ToSpanTrait<[T; SIZE], T>,
> of IntoIterator<@[T; SIZE]> {
type IntoIter = crate::array::SpanIter<T>;
fn into_iter(self: @[T; SIZE]) -> Self::IntoIter {
Expand Down
27 changes: 25 additions & 2 deletions corelib/src/test/array_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn test_array_iterator() {
}

#[test]
fn test_fixed_size_array_iterator() {
fn test_snapshot_fixed_size_array_iterator() {
let mut iter = (@[10_usize, 11, 12, 13]).into_iter();
assert_eq!(iter.next(), Option::Some(@10));
assert_eq!(iter.next(), Option::Some(@11));
Expand All @@ -208,12 +208,35 @@ fn test_fixed_size_array_iterator() {
}

#[test]
fn test_empty_fixed_size_array_iterator() {
fn test_empty_snapshot_fixed_size_array_iterator() {
let mut input: [usize; 0] = [];
let mut iter = (@input).into_iter();
assert!(iter.next().is_none());
}


fn test_snapshot_array_into_iter() {
let mut iter = (@array![1, 2, 3, 4, 5]).into_iter();
assert_eq!(iter.next(), Option::Some(@1));
assert_eq!(iter.next(), Option::Some(@2));
assert_eq!(iter.next(), Option::Some(@3));
assert_eq!(iter.next(), Option::Some(@4));
assert_eq!(iter.next(), Option::Some(@5));
assert!(iter.next().is_none());
}

#[test]
fn test_snapshot_span_into_iter() {
let mut iter = (@(array![1, 2, 3, 4, 5].span())).into_iter();
assert_eq!(iter.next(), Option::Some(@1));
assert_eq!(iter.next(), Option::Some(@2));
assert_eq!(iter.next(), Option::Some(@3));
assert_eq!(iter.next(), Option::Some(@4));
assert_eq!(iter.next(), Option::Some(@5));
assert!(iter.next().is_none());
}

#[test]
fn test_array_into_span() {
assert_eq!(array![1, 2, 3].span(), array![1, 2, 3].into())
}
Expand Down