Skip to content

Commit

Permalink
Fix PrefixInput read (#582)
Browse files Browse the repository at this point in the history
* docs: fix typo

* fix: PrefixInput read

---------

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
kalaninja and bkchr authored Apr 30, 2024
1 parent faac8cc commit fa95ed8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit fa95ed8

Please sign in to comment.