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): Iterator::collect (draft) #7086

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

julio4
Copy link
Contributor

@julio4 julio4 commented Jan 15, 2025

Implementing collect on Iterator.
Currently a draft PR because I'm having some issues.

fn collect<B, +FromIterator<B, Self::Item>, +Drop<T>>(
    self: T,
) -> B {
    FromIterator::from_iter(self)
}

error: Trait has no implementation in context: Iterator::<T>

Not sure why it's not doing the bound correctly as we're in the default implementation of trait Iterator<T>, so we're sure T is implementing Iterator.

I tried to force trait bound:

fn collect<B, +Iterator<T>, +FromIterator<B, Self::Item>, +Drop<T>>(
    self: T,
) -> B {
    FromIterator::from_iter(self)
}

warning: In a trait, paths of the same trait are not allowed. Did you mean to use Self::?
error: Type mismatch: Iterator::Item and _::Item.

Then tried to enforce bounds on Items:

fn collect<B, impl TIter: Iterator<T>, +FromIterator<B, TIter::Item>, +Drop<T>>(
    self: T,
) -> B {
    FromIterator::from_iter(self)
}

warning: In a trait, paths of the same trait are not allowed. Did you mean to use Self::?
Error: Compilation failed.

The compilation fails without any specific error.

If I replace +Iterator<T> with impl TIter: Self it always fails with error: Trait has no implementation in context: core::iter::traits::iterator::Iterator::<T>.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first one is definitely the correct one - but some inference is probably missing.
added some specific suggestions.

Reviewed 1 of 3 files at r1, all commit messages.
Reviewable status: 1 of 3 files reviewed, 4 unresolved discussions (waiting on @julio4)


corelib/src/test/iter_test.cairo line 17 at r1 (raw file):

    assert_eq!(*arr[0], 0);
    assert_eq!(*arr[1], 1);
    assert_eq!(*arr[2], 2);

Suggestion:

    assert_eq!((0..3_u32).into_iter().collect(), array![0, 1, 2]);

corelib/src/iter/traits/iterator.cairo line 99 at r1 (raw file):

    #[inline]
    #[must_use]
    fn collect<B, +FromIterator<B, Self::Item>, +Drop<T>>(

Suggestion:

    fn collect<B, +FromIterator<B, Self::Item>, +Destruct<T>>(

corelib/src/iter/traits/iterator.cairo line 99 at r1 (raw file):

    #[inline]
    #[must_use]
    fn collect<B, +FromIterator<B, Self::Item>, +Drop<T>>(

doc


corelib/src/iter/traits/iterator.cairo line 102 at r1 (raw file):

        self: T,
    ) -> B {
        FromIterator::from_iter(self)

Suggestion:

        FromIterator::<B, Self::Item>::from_iter::<T, Self>(self)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants