Skip to content

Commit

Permalink
feat(corelib): IntoIter for Span-convertible @C
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoIover committed Jan 5, 2025
1 parent eabc3e0 commit d6f8fbd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
7 changes: 7 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 core::iter::IntoIterator<@Span<T>> {
type IntoIter = core::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
11 changes: 5 additions & 6 deletions corelib/src/iter/traits/iterator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ impl IteratorIntoIterator<T, +Iterator<T>> of IntoIterator<T> {
}
}

impl SnapshotFixedSizeArrayIntoIterator<
T, const SIZE: usize, +Drop<T>, impl ToSpan: core::array::ToSpanTrait<[T; SIZE], T>,
> of IntoIterator<@[T; SIZE]> {
type IntoIter = crate::array::SpanIter<T>;
fn into_iter(self: @[T; SIZE]) -> Self::IntoIter {
ToSpan::span(self).into_iter()
impl SnapshotIteratorSpanBased<C, T, +Into<@C, Span<T>>> of core::iter::IntoIterator<@C> {
type IntoIter = core::array::SpanIter<T>;
fn into_iter(self: @C) -> Self::IntoIter {
let span: Span<T> = self.into();
span.into_iter()
}
}
38 changes: 33 additions & 5 deletions corelib/src/test/array_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,50 @@ fn test_array_iterator() {
}

#[test]
fn test_fixed_size_array_iterator() {
let mut iter = (@[10_usize, 11, 12, 13]).into_iter();
fn test_snapshot_fixed_size_array_iterator() {
let fixed_arr = [10_usize, 11, 12, 13];
let mut iter = (@fixed_arr).into_iter();
assert_eq!(iter.next(), Option::Some(@10));
assert_eq!(iter.next(), Option::Some(@11));
assert_eq!(iter.next(), Option::Some(@12));
assert_eq!(iter.next(), Option::Some(@13));
assert!(iter.next().is_none());

assert_eq!(fixed_arr.span()[1], @11);
}

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

assert_eq!(fixed_arr.span().len(), 0);
}


#[test]
fn test_snapshot_array_into_iter() {
let mut arr = array![1, 2, 3, 4, 5];
let mut arr_iter = (@arr).into_iter();

let next = arr_iter.next();
assert!(next == Option::Some(@1));

assert!(arr[1] == @2);
}

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

assert!(span[1] == @2);
}

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

0 comments on commit d6f8fbd

Please sign in to comment.