-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
Signed-off-by: Lai Jiangshan <[email protected]>
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
#[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)) |
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.
Can this just be replaced with self.iter.next().map(self.f)
?
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.
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).
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.
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).
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.
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?
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.
Ah sorry I was under the impression that this was possible when it in fact isn't! I've opened #23015 on the topic.
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.
Ok, I will leave this and look forward to your implementing.
⌛ Testing commit cd65156 with merge ffe3976... |
⌛ Testing commit cd65156 with merge 7003120... |
💔 Test failed - auto-linux-64-x-android-t |
@bors: retry |
⚡ Previous build results are reusable. Rebuilding only auto-linux-32-nopt-t, auto-linux-32-opt, auto-linux-64-nopt-t, auto-linux-64-opt, auto-linux-64-x-android-t, auto-mac-32-opt, auto-mac-64-nopt-t, auto-mac-64-opt, auto-win-32-nopt-t, auto-win-32-opt, auto-win-64-nopt-t, auto-win-64-opt... |
💔 Test failed - auto-win-64-opt |
@bors: retry |
@bors: clean |
Signed-off-by: Lai Jiangshan <[email protected]>
…excrichton Signed-off-by: Lai Jiangshan <[email protected]>
Ah no the "hack" was doing the match manually, which that PR didn't change. My git-blame-foo is weak, and I gotta go. Hopefully someone else can figure it out. |
(The code has been that way since it was introduced, in 8bf9fc5.) |
I'm pretty sure I did that |
Signed-off-by: Lai Jiangshan [email protected]