-
Notifications
You must be signed in to change notification settings - Fork 310
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
Specialize WithPosition::fold
#772
Specialize WithPosition::fold
#772
Conversation
a3edd8d
to
9984eb0
Compare
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.
Hi @Philippe-Cholet! Nice idea.
src/with_position.rs
Outdated
{ | ||
if !self.handled_first { | ||
// Haven't seen the first item yet, and there might be one to give. | ||
self.handled_first = true; |
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.
Do we actually need to set handled_first=true
? self
is consumed and handled_first
seems unused afterwards.
src/with_position.rs
Outdated
} | ||
} | ||
} | ||
if let Some(mut head) = self.peekable.next() { |
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'm somehow bothered because we call peekable.next
here, even though we called peekable.peek
some lines before. I'm not sure if we can easily avoid this (without convoluting control flow too much)...
Maybe something like this (only a draft - didn't test it):
if let Some(head) = if !handled_first {
if let Some(first) = self.peekable.next() {
match self.peekable.next() {
Some(head) => {
init = {
f(init, (Position::First, first));
Some(head)
}
}
None => return f(init, (Position::Only, first)),
}
}
} else {
self.peekable.next()
} {
// Have seen the first item, and there's something left.
init = self.peekable.fold(init, |acc, mut item| {
std::mem::swap(&mut head, &mut item);
f(acc, (Position::Middle, item))
});
// The "head" is now the last item.
init = f(init, (Position::Last, head));
}
@Philippe-Cholet You can use your judgement here.
9984eb0
to
0c2ffa7
Compare
@phimuemue
It bothered me a bit too to |
Just adding that I initially tried to avoid |
I do not see any reason not to and I need this for the next commit.
0c2ffa7
to
4742e9f
Compare
Nicely done, thanks. As an aside: Does the mail address from your commits work? |
@phimuemue I did not check the mail address for quite some time, but I definitely did this time. |
Each call to
f
is done with aPosition
variant known at compile time so I think it might be optimized away in some cases?We should mention #755 in PRs that specialize
fold
(such as this one) to help keep track of the advancement of the TODO list I'm gonna make there.