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): Extend trait #7132

Merged
merged 5 commits into from
Feb 12, 2025
Merged
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
18 changes: 17 additions & 1 deletion corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ impl SnapshotArrayIntoIterator<T> of crate::iter::IntoIterator<@Array<T>> {
}

impl ArrayFromIterator<T, +Drop<T>> of crate::iter::FromIterator<Array<T>, T> {
fn from_iter<I, +Iterator<I>[Item: T], +Destruct<I>>(mut iter: I) -> Array<T> {
fn from_iter<I, +Iterator<I>[Item: T], +Destruct<I>>(iter: I) -> Array<T> {
let mut arr = array![];
for elem in iter {
arr.append(elem);
Expand All @@ -867,6 +867,22 @@ impl ArrayFromIterator<T, +Drop<T>> of crate::iter::FromIterator<Array<T>, T> {
}
}

impl ArrayExtend<T, +Drop<T>> of crate::iter::Extend<Array<T>, T> {
fn extend<
I,
impl IntoIter: IntoIterator<I>,
+TypeEqual<IntoIter::Iterator::Item, T>,
+Destruct<IntoIter::IntoIter>,
+Destruct<I>,
>(
ref self: Array<T>, iter: I,
) {
for elem in iter.into_iter() {
self.append(elem);
};
}
}

/// 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.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,4 @@
mod adapters;
mod traits;
pub use adapters::PeekableTrait;
pub use traits::{FromIterator, IntoIterator, Iterator, Product, Sum};
pub use traits::{Extend, FromIterator, IntoIterator, Iterator};
2 changes: 1 addition & 1 deletion corelib/src/iter/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ mod accum;
mod collect;
mod iterator;
pub use accum::{Product, Sum};
pub use collect::{FromIterator, IntoIterator};
pub use collect::{Extend, FromIterator, IntoIterator};
pub use iterator::Iterator;
36 changes: 36 additions & 0 deletions corelib/src/iter/traits/collect.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::metaprogramming::TypeEqual;

/// Conversion from an [`Iterator`].
///
/// By implementing `FromIterator` for a type, you define how it will be
Expand Down Expand Up @@ -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<T, A> {
/// Extends a collection with the contents of an iterator.
fn extend<
I,
impl IntoIter: IntoIterator<I>,
+TypeEqual<IntoIter::Iterator::Item, A>,
+Destruct<IntoIter::IntoIter>,
+Destruct<I>,
>(
ref self: T, iter: I,
);
}

8 changes: 8 additions & 0 deletions corelib/src/test/array_test.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::test::test_utils::assert_eq;
use crate::iter::Extend;

#[test]
fn test_array() {
Expand Down Expand Up @@ -242,6 +243,13 @@ fn test_array_from_iterator() {
assert_eq!(v, array![0, 1, 2, 3, 4]);
}

#[test]
fn test_array_extend() {
let mut arr = array![1_u32, 2, 3];
arr.extend(4..6_u32);
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())
Expand Down