fix logic error in fill_buffer (#336)
This commit is contained in:
parent
4a57853a3b
commit
00c43bf3b1
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "bincode"
|
||||
version = "1.3.0" # remember to update html_root_url
|
||||
version = "1.3.1" # remember to update html_root_url
|
||||
authors = ["Ty Overby <ty@pre-alpha.com>", "Francesco Mazzoli <f@mazzo.li>", "David Tolnay <dtolnay@gmail.com>", "Zoey Riordan <zoey@dos.cafe>"]
|
||||
exclude = ["logo.png", "examples/*", ".gitignore", ".travis.yml"]
|
||||
|
||||
|
|
|
|||
|
|
@ -141,12 +141,7 @@ where
|
|||
R: io::Read,
|
||||
{
|
||||
fn fill_buffer(&mut self, length: usize) -> Result<()> {
|
||||
// Reserve and fill extra space if needed
|
||||
let current_length = self.temp_buffer.len();
|
||||
if length > current_length {
|
||||
self.temp_buffer.reserve_exact(length - current_length);
|
||||
self.temp_buffer.resize(length, 0);
|
||||
}
|
||||
self.temp_buffer.resize(length, 0);
|
||||
|
||||
self.reader.read_exact(&mut self.temp_buffer)?;
|
||||
|
||||
|
|
@ -185,3 +180,23 @@ where
|
|||
visitor.visit_bytes(&self.temp_buffer[..])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::IoReader;
|
||||
|
||||
#[test]
|
||||
fn test_fill_buffer() {
|
||||
let buffer = vec![0u8; 64];
|
||||
let mut reader = IoReader::new(buffer.as_slice());
|
||||
|
||||
reader.fill_buffer(20).unwrap();
|
||||
assert_eq!(20, reader.temp_buffer.len());
|
||||
|
||||
reader.fill_buffer(30).unwrap();
|
||||
assert_eq!(30, reader.temp_buffer.len());
|
||||
|
||||
reader.fill_buffer(5).unwrap();
|
||||
assert_eq!(5, reader.temp_buffer.len());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
//! Support for `i128` and `u128` is automatically enabled on Rust toolchains
|
||||
//! greater than or equal to `1.26.0` and disabled for targets which do not support it
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/bincode/1.3.0")]
|
||||
#![doc(html_root_url = "https://docs.rs/bincode/1.3.1")]
|
||||
#![crate_name = "bincode"]
|
||||
#![crate_type = "rlib"]
|
||||
#![crate_type = "dylib"]
|
||||
|
|
|
|||
|
|
@ -880,3 +880,21 @@ fn test_varint_length_prefixes() {
|
|||
(1 + std::mem::size_of::<u32>()) as u64
|
||||
); // 2 ** 16 + 1
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_vec_struct() {
|
||||
#[derive(PartialEq, Eq, Clone, Serialize, Deserialize, Debug)]
|
||||
struct ByteVecs {
|
||||
a: Vec<u8>,
|
||||
b: Vec<u8>,
|
||||
c: Vec<u8>,
|
||||
};
|
||||
|
||||
let byte_struct = ByteVecs {
|
||||
a: vec![2; 20],
|
||||
b: vec![3; 30],
|
||||
c: vec![1; 10],
|
||||
};
|
||||
|
||||
the_same(byte_struct);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue