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::Sum for snapshot of addable types #7235

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

Conversation

cairolover
Copy link
Contributor

enables usages like:

    let span = array![4_u8, 5, 6].span();
    println!("sum of span: {}", span.into_iter().sum());

@reviewable-StarkWare
Copy link

This change is Reviewable

@cairolover cairolover changed the title feat(corelib): Sum for snapshot of addable types feat(corelib): Iterator::Sum for snapshot of addable types Feb 6, 2025
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.

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @cairolover)


a discussion (no related file):
i don't really think we should have this.

we should probably just have a .cloned() and .copied() adapters - and then .copied().sum() would provide this.

Copy link
Contributor Author

@cairolover cairolover left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi)


a discussion (no related file):

Previously, orizi wrote…

i don't really think we should have this.

we should probably just have a .cloned() and .copied() adapters - and then .copied().sum() would provide this.

Would you be able to provide some guidance on that? Here's my idea:

#[derive(Drop, Clone)]
#[must_use]
pub struct Copied<I> {
    iter: I,
}

pub fn copied_iterator<I>(iter: I) -> Copied<I> {
    Copied { iter }
}

impl CopiedIterator<I, impl TIter: Iterator<I>, +Destruct<I>, +Copy<TIter::Item>> of Iterator<Copied<I>> {
    type Item = TIter::Item;

    fn next(ref self: Copied<I>) -> Option<Self::Item> {
        self.iter.next().copied()
    }
}

^ For that I need a copied method on Option

I'm not quite sure how to implement such a method that would accept as input T and @T at the same time.

    fn copied<+Copy<T>>(self: Option<@T>) -> Option<T> {
        match self {
            Some(x) => Some(*x),
            None => None,
        }
    }

How can I know at compile-time whether i'm operating on a snapshot or not?

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.

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @cairolover)


a discussion (no related file):

Previously, cairolover (cairolover) wrote…

Would you be able to provide some guidance on that? Here's my idea:

#[derive(Drop, Clone)]
#[must_use]
pub struct Copied<I> {
    iter: I,
}

pub fn copied_iterator<I>(iter: I) -> Copied<I> {
    Copied { iter }
}

impl CopiedIterator<I, impl TIter: Iterator<I>, +Destruct<I>, +Copy<TIter::Item>> of Iterator<Copied<I>> {
    type Item = TIter::Item;

    fn next(ref self: Copied<I>) -> Option<Self::Item> {
        self.iter.next().copied()
    }
}

^ For that I need a copied method on Option

I'm not quite sure how to implement such a method that would accept as input T and @T at the same time.

    fn copied<+Copy<T>>(self: Option<@T>) -> Option<T> {
        match self {
            Some(x) => Some(*x),
            None => None,
        }
    }

How can I know at compile-time whether i'm operating on a snapshot or not?

i think you can probably do it all using a map call.
and maybe just request an implementation of Deref for the item type.
this would probably be more extended then just copied though - but i need to think some

Copy link
Contributor Author

@cairolover cairolover left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi)


a discussion (no related file):

Previously, orizi wrote…

i think you can probably do it all using a map call.
and maybe just request an implementation of Deref for the item type.
this would probably be more extended then just copied though - but i need to think some

I tried something but could not get a good result, sorry

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.

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @cairolover)


a discussion (no related file):

Previously, cairolover (cairolover) wrote…

I tried something but could not get a good result, sorry

should be very similar to this:
https://doc.rust-lang.org/beta/std/iter/trait.Iterator.html#method.copied

just adding a struct with an iterator as an adapter, requesting the element to be +Copy<Self::Item>

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