Skip to content

Commit

Permalink
feat(corelib): Extend trait
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jan 21, 2025
1 parent cdd3176 commit 4c56ebb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
18 changes: 17 additions & 1 deletion corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,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 @@ -869,6 +869,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 @@ -231,4 +231,4 @@
//! [`map`]: Iterator::map
mod adapters;
mod traits;
pub use traits::{FromIterator, IntoIterator, Iterator};
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
@@ -1,4 +1,4 @@
mod collect;
mod iterator;
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,
);
}

15 changes: 15 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,20 @@ fn test_array_from_iterator() {
assert_eq!(v, array![0, 1, 2, 3, 4]);
}

#[test]
fn test_array_extend() {
let mut arr: Array<u32> = 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<u32> = 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())
Expand Down

0 comments on commit 4c56ebb

Please sign in to comment.