-
Notifications
You must be signed in to change notification settings - Fork 14k
stabilize Peekable::next_if_map (#![feature(peekable_next_if_map)])
#148941
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
base: main
Are you sure you want to change the base?
Conversation
Peekable::map_next_ifPeekable::map_next_if (#![feature(peekable_next_if_map)])
Peekable::map_next_if (#![feature(peekable_next_if_map)])Peekable::next_if_map (#![feature(peekable_next_if_map)])
|
r? libs-api |
|
IIRC stabilisation reports are often looking for personal experiences with an API? In my case, I haven't been using Here's an example of the sort of code I ended up writing, due to not having while let Some(nybble) = iter.next_if(|b| b.is_ascii_hexdigit()) {
payload.push(Nybble::from_hexdigit(nybble as char).expect(
"ASCII hexdigits are valid nybbles"));
}but this really would be much better written as while let Some(nybble) = iter.next_if_map(|b| Nybble::from_hexdigit(*b as char)) {
payload.push(nybble);
}which avoids the double check. (In this particular case there are no TOCTOU issues because nothing can change whether a particular byte is a valid ASCII hex digit, but checking a condition twice is a code smell and occasionally a security vulnerability, so it is generally considered good style to check only once and The existing API isn't ideal for my use-case, though – the previous code-block doesn't actually match the signature of the while let Some(nybble) = iter.next_if_map(|b| Nybble::from_hexdigit(b as char).ok_or(b)) {
payload.push(nybble);
}which is still cleaner than the code without The API proposed for stabilisation also makes it possible to change the element in the iterator's peek slot (because nothing forces the I guess the correct API depends on how often |
|
fair enough, and thanks for your thoughts. I couldn't find that many uses on github yet, so these kinds of experiences help <3. I personally used it a bunch too now, specifically for parsers as I mentioned. I had some kinds of tokens that contained contents: like strings containing a reference to the contents of the string. Matching on those tokens and getting the contents out is impossible to do as neatly as with this API. In fact, the entire pattern became a single line :) |
Stabilization report
Summary
#![feature(peekable_next_if_map)]is a variation ofnext_ifon peekable iterators that can transform the peeked item. This creates a way to take ownership of the next item in an iterator when some condition holds, but put the item back when the condition doesn't hold. This pattern would otherwise have needed unwraps in many cases.Tracking issue
What is stabilized
Example usage adapted from the ACP:
Nightly use
At the moment, this feature is barely used in nightly, though I've found multiple good uses for it in my own projects, hence my pushing for stabilization. It makes the kind of patterns used in recursive descent parsing super concise and maybe with its stabilization it will find more use.
Test coverage
Besides a quite comprehensive doctest, this feature is tested (including panicking in the closure) here:
rust/library/coretests/tests/iter/adapters/peekable.rs
Lines 275 to 359 in c880acd
History
Acknowledgments
ACP, implementation and tracking issue for this feature all by @kennytm <3