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

iter: use Option::map() in struct Iterater::map() #22958

Merged
merged 1 commit into from
Mar 4, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,24 +1565,13 @@ pub struct Map<I, F> {
f: F,
}

impl<I: Iterator, F, B> Map<I, F> where F: FnMut(I::Item) -> B {
#[inline]
fn do_map(&mut self, elt: Option<I::Item>) -> Option<B> {
match elt {
Some(a) => Some((self.f)(a)),
_ => None
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<B, I: Iterator, F> Iterator for Map<I, F> where F: FnMut(I::Item) -> B {
type Item = B;

#[inline]
fn next(&mut self) -> Option<B> {
let next = self.iter.next();
self.do_map(next)
self.iter.next().map(|a| (self.f)(a))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this just be replaced with self.iter.next().map(self.f)?

Copy link
Author

Choose a reason for hiding this comment

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

self.f can not be moved.
if we have "impl FnMut for &FnMut", then we can use &self.iter.next().map(&self.f)

I'm curious that it is not implemented yet.
I'm also curious that fn(Arg) is not trait Fn(Arg). If it is implemented, it also helps (in other code).

Copy link
Member

Choose a reason for hiding this comment

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

I believe this should be possible with self.iter.next().map(&mut self.f). The fn types are distinct from the Fn traits because fn is a specific type for a function pointer (but implements the Fn trait).

We cannot implement FnMut for &FnMut, but it is implemented for &mut FnMut (as the mutable borrow is required).

Copy link
Author

Choose a reason for hiding this comment

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

Due to the auto implmeting among the Fn, FnMut, FnOnce, we have some constrains.

Among these 3 types: &mut T where T: Fn, &mut T where T:FnMut, &mut T where T:FnOnce, we can only impl one Fn* trait to them, otherwise they will conflict. The only choice is impl FnMut for &mut T where T:FnMut, other choices make no sense.

The same, Among these 3 types: &T where T: Fn, &T where T:FnMut, &T where T:FnOnce, we can only impl one Fn* trait to them, otherwise they will conflict. The best choice is impl Fn for &T where T:Fn, other choices will cover less situation.

Could I implement the above?

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry I was under the impression that this was possible when it in fact isn't! I've opened #23015 on the topic.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I will leave this and look forward to your implementing.

}

#[inline]
Expand All @@ -1597,8 +1586,7 @@ impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F> where
{
#[inline]
fn next_back(&mut self) -> Option<B> {
let next = self.iter.next_back();
self.do_map(next)
self.iter.next_back().map(|a| (self.f)(a))
}
}

Expand All @@ -1613,8 +1601,7 @@ impl<B, I: RandomAccessIterator, F> RandomAccessIterator for Map<I, F> where

#[inline]
fn idx(&mut self, index: usize) -> Option<B> {
let elt = self.iter.idx(index);
self.do_map(elt)
self.iter.idx(index).map(|a| (self.f)(a))
}
}

Expand Down