Discard leftover input when consumed (#76)

* Discard leftover input when consumed

The iterator given to `parse_event` now `take()` the value from
`leftover`.

* Always save leftover

`parse_event` will consume it when required.

* Remove OptionIterator, directly uses std::option::Iter
This commit is contained in:
Alexandre Bury 2016-12-18 00:49:40 +01:00 committed by ticki
parent 00ca758d45
commit c2df3d7edd
1 changed files with 8 additions and 8 deletions

View File

@ -56,14 +56,14 @@ impl<R: Read> Iterator for Events<R> {
c => parse_event(Ok(c), &mut source.bytes()), c => parse_event(Ok(c), &mut source.bytes()),
}, },
Ok(2) => { Ok(2) => {
if buf[0] != b'\x1B' { let mut option_iter = &mut Some(buf[1]).into_iter();
// this is not an escape sequence, but we read two bytes, save the second byte let result = {
// for later let mut iter = option_iter.map(|c| Ok(c)).chain(source.bytes());
self.leftover = Some(buf[1]);
}
let mut iter = buf[1..2].iter().map(|c| Ok(*c)).chain(source.bytes());
parse_event(Ok(buf[0]), &mut iter) parse_event(Ok(buf[0]), &mut iter)
};
// If the option_iter wasn't consumed, keep the byte for later.
self.leftover = option_iter.next();
result
} }
Ok(_) => unreachable!(), Ok(_) => unreachable!(),
Err(e) => Err(e), Err(e) => Err(e),