diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 4d83548fb8a..f28096a1102 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -816,6 +816,13 @@ impl SpanIntoIterator of crate::iter::IntoIterator> { } } +impl SnapshotSpanIntoIterator of core::iter::IntoIterator<@Span> { + type IntoIter = core::array::SpanIter; + fn into_iter(self: @Span) -> Self::IntoIter { + (*self).into_iter() + } +} + /// An iterator struct over an array collection. #[derive(Drop)] pub struct ArrayIter { diff --git a/corelib/src/iter/traits/iterator.cairo b/corelib/src/iter/traits/iterator.cairo index 383b70767c5..b4545124acb 100644 --- a/corelib/src/iter/traits/iterator.cairo +++ b/corelib/src/iter/traits/iterator.cairo @@ -23,11 +23,10 @@ impl IteratorIntoIterator> of IntoIterator { } } -impl SnapshotFixedSizeArrayIntoIterator< - T, const SIZE: usize, +Drop, impl ToSpan: core::array::ToSpanTrait<[T; SIZE], T>, -> of IntoIterator<@[T; SIZE]> { - type IntoIter = crate::array::SpanIter; - fn into_iter(self: @[T; SIZE]) -> Self::IntoIter { - ToSpan::span(self).into_iter() +impl SnapshotIteratorSpanBased>> of core::iter::IntoIterator<@C> { + type IntoIter = core::array::SpanIter; + fn into_iter(self: @C) -> Self::IntoIter { + let span: Span = self.into(); + span.into_iter() } } diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index 08889072e2a..6e3d883a8c6 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -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()) }