diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index fb2da32f6da..f5261b34ba9 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -816,6 +816,13 @@ impl SpanIntoIterator of crate::iter::IntoIterator> { } } +impl SnapshotSpanIntoIterator of crate::iter::IntoIterator<@Span> { + type IntoIter = crate::array::SpanIter; + fn into_iter(self: @Span) -> Self::IntoIter { + (*self).into_iter() + } +} + /// An iterator struct over an array collection. #[derive(Drop)] pub struct ArrayIter { @@ -845,6 +852,13 @@ impl ArrayIntoIterator of crate::iter::IntoIterator> { } } +impl SnapshotArrayIntoIterator of crate::iter::IntoIterator<@Array> { + type IntoIter = SpanIter; + fn into_iter(self: @Array) -> Self::IntoIter { + self.span().into_iter() + } +} + /// Information about a fixed-sized array. trait FixedSizedArrayInfo { /// The type of the elements in the array. diff --git a/corelib/src/iter/traits/collect.cairo b/corelib/src/iter/traits/collect.cairo index 90a02b3962b..2bd46f011da 100644 --- a/corelib/src/iter/traits/collect.cairo +++ b/corelib/src/iter/traits/collect.cairo @@ -97,7 +97,7 @@ impl IteratorIntoIterator> of IntoIterator { } impl SnapshotFixedSizeArrayIntoIterator< - T, const SIZE: usize, +Drop, impl ToSpan: core::array::ToSpanTrait<[T; SIZE], T>, + T, const SIZE: usize, +Drop, impl ToSpan: crate::array::ToSpanTrait<[T; SIZE], T>, > of IntoIterator<@[T; SIZE]> { type IntoIter = crate::array::SpanIter; fn into_iter(self: @[T; SIZE]) -> Self::IntoIter { diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index 08889072e2a..37152614ba2 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -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)); @@ -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()) }