From c2df3d7edd01566c1db3c110615df27c210726be Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sun, 18 Dec 2016 00:49:40 +0100 Subject: [PATCH] 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 --- src/input.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/input.rs b/src/input.rs index 26abd3e..a06a411 100644 --- a/src/input.rs +++ b/src/input.rs @@ -56,14 +56,14 @@ impl Iterator for Events { c => parse_event(Ok(c), &mut source.bytes()), }, Ok(2) => { - if buf[0] != b'\x1B' { - // this is not an escape sequence, but we read two bytes, save the second byte - // for later - 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) + let mut option_iter = &mut Some(buf[1]).into_iter(); + let result = { + let mut iter = option_iter.map(|c| Ok(c)).chain(source.bytes()); + 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!(), Err(e) => Err(e),