Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
15 changes: 14 additions & 1 deletion src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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..])
},
Expand Down Expand Up @@ -693,6 +696,16 @@ impl Decode for Compact<u128> {
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 = [
Expand Down