diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 25413d17bfe..81668428952 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -860,7 +860,7 @@ impl SnapshotArrayIntoIterator of crate::iter::IntoIterator<@Array> { } impl ArrayFromIterator> of crate::iter::FromIterator, T> { - fn from_iter[Item: T], +Destruct>(mut iter: I) -> Array { + fn from_iter[Item: T], +Destruct>(iter: I) -> Array { let mut arr = array![]; for elem in iter { arr.append(elem); @@ -869,6 +869,22 @@ impl ArrayFromIterator> of crate::iter::FromIterator, T> { } } +impl ArrayExtend> of crate::iter::Extend, T> { + fn extend< + I, + impl IntoIter: IntoIterator, + +TypeEqual, + +Destruct, + +Destruct, + >( + ref self: Array, iter: I, + ) { + for elem in iter.into_iter() { + self.append(elem); + }; + } +} + /// Information about a fixed-sized array. trait FixedSizedArrayInfo { /// The type of the elements in the array. diff --git a/corelib/src/iter.cairo b/corelib/src/iter.cairo index 527313a5d77..95e635755c7 100644 --- a/corelib/src/iter.cairo +++ b/corelib/src/iter.cairo @@ -231,4 +231,4 @@ //! [`map`]: Iterator::map mod adapters; mod traits; -pub use traits::{FromIterator, IntoIterator, Iterator}; +pub use traits::{Extend, FromIterator, IntoIterator, Iterator}; diff --git a/corelib/src/iter/traits.cairo b/corelib/src/iter/traits.cairo index da84228bedb..1be7af0366d 100644 --- a/corelib/src/iter/traits.cairo +++ b/corelib/src/iter/traits.cairo @@ -1,4 +1,4 @@ mod collect; mod iterator; -pub use collect::{FromIterator, IntoIterator}; +pub use collect::{Extend, FromIterator, IntoIterator}; pub use iterator::Iterator; diff --git a/corelib/src/iter/traits/collect.cairo b/corelib/src/iter/traits/collect.cairo index a8a1ef534b1..a468a3b2a0f 100644 --- a/corelib/src/iter/traits/collect.cairo +++ b/corelib/src/iter/traits/collect.cairo @@ -1,3 +1,5 @@ +use crate::metaprogramming::TypeEqual; + /// Conversion from an [`Iterator`]. /// /// By implementing `FromIterator` for a type, you define how it will be @@ -195,3 +197,37 @@ impl SnapshotFixedSizeArrayIntoIterator< ToSpan::span(self).into_iter() } } + +/// Extend a collection with the contents of an iterator. +/// +/// Iterators produce a series of values, and collections can also be thought +/// of as a series of values. The `Extend` trait bridges this gap, allowing you +/// to extend a collection by including the contents of that iterator. When +/// extending a collection with an already existing key, that entry is updated +/// or, in the case of collections that permit multiple entries with equal +/// keys, that entry is inserted. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// let mut arr = array![1, 2]; +/// +/// arr.extend(array![3, 4, 5]); +/// +/// assert_eq!(arr, array![1, 2, 3, 4, 5]); +/// ``` +pub trait Extend { + /// Extends a collection with the contents of an iterator. + fn extend< + I, + impl IntoIter: IntoIterator, + +TypeEqual, + +Destruct, + +Destruct, + >( + ref self: T, iter: I, + ); +} + diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index ffee9f3071b..f549a62846d 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -1,4 +1,5 @@ use crate::test::test_utils::assert_eq; +use crate::iter::Extend; #[test] fn test_array() { @@ -242,6 +243,20 @@ fn test_array_from_iterator() { assert_eq!(v, array![0, 1, 2, 3, 4]); } +#[test] +fn test_array_extend() { + let mut arr: Array = array![1, 2, 3]; + arr.extend((4..6_u32)); + assert_eq!(arr, array![1, 2, 3, 4, 5]); +} + +// #[test] +// fn test_array_extend_panic() { +// let mut arr: Array = array![1, 2, 3]; +// arr.extend((4..6_u32).into_iter()); +// assert_eq!(arr, array![1, 2, 3, 4, 5]); +// } + #[test] fn test_array_into_span() { assert_eq!(array![1, 2, 3].span(), array![1, 2, 3].into())