diff --git a/src/codec.rs b/src/codec.rs index 6beebad2..c8d9e40d 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -75,7 +75,7 @@ pub trait Input { /// Descend into nested reference when decoding. /// This is called when decoding a new refence-based instance, - /// such as `Vec` or `Box`. Currently all such types are + /// such as `Vec` or `Box`. Currently, all such types are /// allocated on the heap. fn descend_ref(&mut self) -> Result<(), Error> { Ok(()) diff --git a/src/compact.rs b/src/compact.rs index 9299db6b..71563d50 100644 --- a/src/compact.rs +++ b/src/compact.rs @@ -64,8 +64,11 @@ impl<'a, T: 'a + Input> Input for PrefixInput<'a, T> { } fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> { + if buffer.is_empty() { + return Ok(()); + } match self.prefix.take() { - Some(v) if !buffer.is_empty() => { + Some(v) => { buffer[0] = v; self.input.read(&mut buffer[1..]) }, @@ -693,6 +696,16 @@ impl Decode for Compact { mod tests { use super::*; + #[test] + fn prefix_input_empty_read_unchanged() { + let mut input = PrefixInput { prefix: Some(1), input: &mut &vec![2, 3, 4][..] }; + assert_eq!(input.remaining_len(), Ok(Some(4))); + let mut empty_buf = []; + assert_eq!(input.read(&mut empty_buf[..]), Ok(())); + assert_eq!(input.remaining_len(), Ok(Some(4))); + assert_eq!(input.read_byte(), Ok(1)); + } + #[test] fn compact_128_encoding_works() { let tests = [