Skip to content

Commit

Permalink
Merge branch 'main' into feat/iter_adapter_map
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 authored Jan 7, 2025
2 parents 978c723 + a12d7d0 commit 9b15a82
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 35 deletions.
4 changes: 2 additions & 2 deletions corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ impl SpanIterator<T> of Iterator<SpanIter<T>> {

impl SpanIntoIterator<T> of crate::iter::IntoIterator<Span<T>> {
type IntoIter = SpanIter<T>;
fn into_iter(self: Span<T>) -> SpanIter<T> {
fn into_iter(self: Span<T>) -> Self::IntoIter {
SpanIter { span: self }
}
}
Expand Down Expand Up @@ -840,7 +840,7 @@ impl ArrayIterator<T> of Iterator<ArrayIter<T>> {

impl ArrayIntoIterator<T> of crate::iter::IntoIterator<Array<T>> {
type IntoIter = ArrayIter<T>;
fn into_iter(self: Array<T>) -> ArrayIter<T> {
fn into_iter(self: Array<T>) -> Self::IntoIter {
ArrayIter { array: self }
}
}
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/byte_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ impl ByteArrayIterator of crate::iter::Iterator<ByteArrayIter> {
impl ByteArrayIntoIterator of crate::iter::IntoIterator<ByteArray> {
type IntoIter = ByteArrayIter;
#[inline]
fn into_iter(self: ByteArray) -> ByteArrayIter {
fn into_iter(self: ByteArray) -> Self::IntoIter {
ByteArrayIter { current_index: (0..self.len()).into_iter(), ba: self }
}
}
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 @@

mod adapters;
mod traits;
pub use traits::iterator::{IntoIterator, Iterator};
pub use traits::{IntoIterator, Iterator};
5 changes: 4 additions & 1 deletion corelib/src/iter/traits.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod iterator;
mod collect;
mod iterator;
pub use collect::IntoIterator;
pub use iterator::Iterator;
106 changes: 106 additions & 0 deletions corelib/src/iter/traits/collect.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/// Conversion into an [`Iterator`].
///
/// By implementing `IntoIterator` for a type, you define how it will be
/// converted to an iterator. This is common for types which describe a
/// collection of some kind.
///
/// One benefit of implementing `IntoIterator` is that your type will [work
/// with Cairo's `for` loop syntax](crate::iter#for-loops-and-intoiterator).
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut iter = array![1, 2, 3].into_iter();
///
/// assert_eq!(Option::Some(1), iter.next());
/// assert_eq!(Option::Some(2), iter.next());
/// assert_eq!(Option::Some(3), iter.next());
/// assert_eq!(Option::None, iter.next());
/// ```
/// Implementing `IntoIterator` for your type:
///
/// ```
/// // A sample collection, that's just a wrapper over Array<u32>
/// #[derive(Drop, Debug)]
/// struct MyCollection {
/// arr: Array<u32>
/// }
///
/// // Let's give it some methods so we can create one and add things
/// // to it.
/// #[generate_trait]
/// impl MyCollectionImpl of MyCollectionTrait {
/// fn new() -> MyCollection {
/// MyCollection {
/// arr: ArrayTrait::new()
/// }
/// }
///
/// fn add(ref self: MyCollection, elem: u32) {
/// self.arr.append(elem);
/// }
/// }
///
/// // and we'll implement IntoIterator
/// impl MyCollectionIntoIterator of IntoIterator<MyCollection> {
/// type IntoIter = crate::array::ArrayIter<u32>;
/// fn into_iter(self: MyCollection) -> Self::IntoIter {
/// self.arr.into_iter()
/// }
/// }
///
/// // Now we can make a new collection...
/// let mut c = MyCollectionTrait::new();
///
/// // ... add some stuff to it ...
/// c.add(0);
/// c.add(1);
/// c.add(2);
///
/// // ... and then turn it into an Iterator:
/// let mut n = 0;
/// for i in c {
/// assert_eq!(i, n);
/// n += 1;
/// };
/// ```
pub trait IntoIterator<T> {
/// The iterator type that will be created.
type IntoIter;
impl Iterator: Iterator<Self::IntoIter>;
/// Creates an iterator from a value.
///
/// See the [module-level documentation] for more.
///
/// [module-level documentation]: crate::iter
///
/// # Examples
///
/// ```
/// let mut iter = array![1, 2, 3].into_iter();
///
/// assert_eq!(Option::Some(1), iter.next());
/// assert_eq!(Option::Some(2), iter.next());
/// assert_eq!(Option::Some(3), iter.next());
/// assert_eq!(Option::None, iter.next());
/// ```
fn into_iter(self: T) -> Self::IntoIter;
}

impl IteratorIntoIterator<T, +Iterator<T>> of IntoIterator<T> {
type IntoIter = T;
fn into_iter(self: T) -> T {
self
}
}

impl SnapshotFixedSizeArrayIntoIterator<
T, const SIZE: usize, +Drop<T>, impl ToSpan: core::array::ToSpanTrait<[T; SIZE], T>,
> of IntoIterator<@[T; SIZE]> {
type IntoIter = crate::array::SpanIter<T>;
fn into_iter(self: @[T; SIZE]) -> Self::IntoIter {
ToSpan::span(self).into_iter()
}
}
25 changes: 0 additions & 25 deletions corelib/src/iter/traits/iterator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,3 @@ pub trait Iterator<T> {
mapped_iterator(self, f)
}
}

/// Turn a collection of values into an iterator.
pub trait IntoIterator<T> {
/// The iterator type that will be created.
type IntoIter;
impl Iterator: Iterator<Self::IntoIter>;
/// Creates an iterator from a collection.
fn into_iter(self: T) -> Self::IntoIter;
}

impl IteratorIntoIterator<T, +Iterator<T>> of IntoIterator<T> {
type IntoIter = T;
fn into_iter(self: T) -> T {
self
}
}

impl SnapshotFixedSizeArrayIntoIterator<
T, const SIZE: usize, +Drop<T>, impl ToSpan: core::array::ToSpanTrait<[T; SIZE], T>,
> of IntoIterator<@[T; SIZE]> {
type IntoIter = crate::array::SpanIter<T>;
fn into_iter(self: @[T; SIZE]) -> Self::IntoIter {
ToSpan::span(self).into_iter()
}
}
20 changes: 17 additions & 3 deletions corelib/src/ops/range.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,23 @@ pub struct Range<T> {
}

#[generate_trait]
pub impl RangeImpl<T, +Copy<T>, +Drop<T>, +PartialOrd<T>> of RangeTrait<T> {
pub impl RangeImpl<T, +Destruct<T>, +PartialOrd<@T>> of RangeTrait<T> {
/// Returns `true` if `item` is contained in the range.
///
/// # Examples
///
/// ```
/// assert!(!(3..5).contains(@2));
/// assert!( (3..5).contains(@3));
/// assert!( (3..5).contains(@4));
/// assert!(!(3..5).contains(@5));
///
/// assert!(!(3..3).contains(@3));
/// assert!(!(3..2).contains(@3));
fn contains(self: @Range<T>, item: @T) -> bool {
self.start <= item && item < self.end
}

/// Returns `true` if the range contains no items.
///
/// # Examples
Expand Down Expand Up @@ -111,7 +127,6 @@ impl RangeIntoIterator<
-SierraIntRangeSupport<T>,
> of IntoIterator<Range<T>> {
type IntoIter = RangeIterator<T>;

fn into_iter(self: Range<T>) -> Self::IntoIter {
let start = self.start;
let end = self.end;
Expand Down Expand Up @@ -159,7 +174,6 @@ impl SierraRangeIntoIterator<
T, +Copy<T>, +Drop<T>, +SierraIntRangeSupport<T>,
> of IntoIterator<Range<T>> {
type IntoIter = internal::IntRange<T>;

fn into_iter(self: Range<T>) -> Self::IntoIter {
match internal::int_range_try_new(self.start, self.end) {
Result::Ok(range) => range,
Expand Down
3 changes: 1 addition & 2 deletions corelib/src/option.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,8 @@ impl OptionIterator<T> of crate::iter::Iterator<OptionIter<T>> {

impl OptionIntoIterator<T> of crate::iter::IntoIterator<Option<T>> {
type IntoIter = OptionIter<T>;

#[inline]
fn into_iter(self: Option<T>) -> OptionIter<T> {
fn into_iter(self: Option<T>) -> Self::IntoIter {
OptionIter { inner: self }
}
}
11 changes: 11 additions & 0 deletions corelib/src/test/range_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ fn test_range_is_empty() {
assert!((3_u8..2_u8).is_empty());
}

#[test]
fn test_range_contains() {
assert!(!(3_u8..5).contains(@2));
assert!((3_u8..5).contains(@3));
assert!((3_u8..5).contains(@4));
assert!(!(3_u8..5).contains(@5));

assert!(!(3_u8..3).contains(@3));
assert!(!(3_u8..2).contains(@3));
}

#[test]
fn test_range_format() {
assert!(format!("{:?}", 1..5) == "1..5");
Expand Down

0 comments on commit 9b15a82

Please sign in to comment.