-
Notifications
You must be signed in to change notification settings - Fork 577
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
There was a problem hiding this 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?
There was a problem hiding this 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 onOption
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
There was a problem hiding this 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 ofDeref
for the item type.
this would probably be more extended then justcopied
though - but i need to think some
I tried something but could not get a good result, sorry
There was a problem hiding this 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>
enables usages like: