You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Similar to Iterator::filter_map, sometimes it is more natural to combine Peekable::next_if and Option::map into a single operation.
Motivating examples or use cases
Consider the following code to unescape octal-escape sequences:
fnunescape_octal(a:&str) -> String{letmut it = Peekable::new(a.chars());letmut res = String::with_capacity(a.len());whileletSome(c) = it.next(){if c == '\\'{letmut codepoint = 0;/*------------------------------------------------------*//* read the following [0-7]* and parse into `codepoint` *//*------------------------------------------------------*/
res.push(char::from_u32(codepoint).unwrap());}else{
res.push(c);}}
res
}assert_eq!(unescape_octal(r"ab\377\570c\144e\146"),"abÿŸcdef");
There are currently 2 ways to implement /* read the following [0-7]* and parse into `codepoint` */, each with some small disadvantages:
Using .next_if(), but it requires evaluating the condition twice and there is an unnecessary .unwrap():
The .next_if()'s condition takes an impl FnOnce(&I::Item) -> bool as input, and returns the item on succeed.
We can create a variant which takes an impl FnOnce(&I::Item) -> Option<R> as input, with Some(r) representing "success" (which the item is consumed) and None "failure".
impl<I:Iterator>Peekable<I>{pubfnmap_next_if_some<R>(&mutself,func:implFnOnce(&I::Item) -> Option<R>) -> Option<R>{let item = self.peek()?;let result = func(item)?;self.peeked = None;// equivalent to `self.next()`Some(result)}}
The motivating example can then be implemented as:
whileletSome(s) = parser.map_next_if_ok(|event| match event {
pulldown_cmark::Event::Text(s) => Ok(s),
other => Err(other),}){do_something(s);}
This is not saying this ACP is supposed to entirely replace .peek_slot() though. For instance the map_next_if approach cannot scale to multiple match arms doing different things like this:
loop{let peek_slot = parser.peek_slot();match peek_slot.take(){Some(pulldown_cmark::Event::Text(text)) => do_something(text),Some(pulldown_cmark::Event::Code(code)) => do_something_else(code),// <-- new branch can be easily added
other => {*peek_slot = other;// <-- still, forgetting to unpeek will be a foot gun.break;}}}
Similar to .peek_slot() the user may unpeek an entirely irrelevant item with Err(custom_value). This is already possible with .peek_mut() (stable since 1.53) so it is not considered a problem.
Links and related work
Some real world example code which can be improved by this ACP (by eliminating some unreachable!() branches or redundant checks)
let transactions = core::iter::from_fn(|| {
spi_operations.map_next_if_ok(|operation| match operation {SpiOperation::Transaction(transaction) => Ok(transaction),
other => Err(other),})});
let source_info = lines.map_next_if_ok(|line| match line {ParsedLine::BacktraceSource(source_info) => Ok(source_info),
other => Err(other),});
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
We think this problem seems worth solving, and the standard library might be the right place to solve it.
We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Proposal
Problem statement
Similar to
Iterator::filter_map, sometimes it is more natural to combinePeekable::next_ifandOption::mapinto a single operation.Motivating examples or use cases
Consider the following code to unescape octal-escape sequences:
There are currently 2 ways to implement
/* read the following [0-7]* and parse into `codepoint` */, each with some small disadvantages:Using
.next_if(), but it requires evaluating the condition twice and there is an unnecessary.unwrap():Using
.peek(), but the user has to remember to call.next()Solution sketch
The
.next_if()'s condition takes animpl FnOnce(&I::Item) -> boolas input, and returns the item on succeed.We can create a variant which takes an
impl FnOnce(&I::Item) -> Option<R>as input, withSome(r)representing "success" (which the item is consumed) andNone"failure".The motivating example can then be implemented as:
Alternatives
I am not too fond of the name
map_next_if_*. I expect better name suggestions.One may also provide an alternative interface which takes an owned value:
This allows us to provide an alternative solution to Add
iter::Peekable::next_unpeek#557:This is not saying this ACP is supposed to entirely replace
.peek_slot()though. For instance themap_next_ifapproach cannot scale to multiple match arms doing different things like this:Similar to
.peek_slot()the user may unpeek an entirely irrelevant item withErr(custom_value). This is already possible with.peek_mut()(stable since 1.53) so it is not considered a problem.Links and related work
Some real world example code which can be improved by this ACP (by eliminating some
unreachable!()branches or redundant checks)https://github.com/rust-lang/rust/blob/8df4a58ac47b778b093652d6190a6f9d54638774/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs#L1290-L1300
https://github.com/max-heller/mdbook-pandoc/blob/197fea7c205ace88a4cda01afa9bd7be983f5b5a/src/preprocess.rs#L1017-L1026
https://github.com/Rapptz/jimaku/blob/262aa0e370dbf74ea61b624cd9ecbe5415d19f15/src/japanese.rs#L259-L270
https://github.com/junelife/esp-idf-hal/blob/7db642c37bc2869b7da71e250340c1f56bc26f04/src/spi.rs#L908-L913
https://github.com/parasyte/myn/blob/ba6980a9edd3c13cca6e1d643fae8c43fea730fd/src/ty.rs#L193-L196
https://github.com/nilehmann/backtracetk/blob/2dfc9d9a554ff8fb672e4cf357dd08d652b30b84/src/lib.rs#L138-L146
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution: