I’m writing a lexer. My lexer implements Iterator<Item=Result<Token, InvalidInput>>
When I find an invalid input that I can’t parse, I would like to flush my internal iterator (it’s a std::iter::CharIndices
over the input string), so that the next time the next()
function is called, it returns None
.
Currently, I’m doing:
while let Some(_) = self.iter.next() {}
return Some(Err(InvalidToken));
This is (I assume) slow, since this is going to be O(n) (unless the compiler optimize it).
Could I do something like:
self.iter.flush();
return Some(Err(InvalidToken));
I took a look at std::iter::Fuse, but I didn’t found such method.
Or do I need to add a boolean in my lexer?