Merge pull request #120 from sourrust/fix/fileproducer-resize

Fix FileProducer::resize
This commit is contained in:
Geoffroy Couprie 2015-11-11 13:03:48 +01:00
commit f2d49b1a7a

View File

@ -270,19 +270,22 @@ impl FileProducer {
/// If the new buffer is smaller, the prefix will be copied, and the rest of the data will be dropped
pub fn resize(&mut self, s: usize) -> usize {
let mut v = Vec::with_capacity(s);
v.extend(repeat(0).take(s));
let length = self.end - self.start;
v.extend(repeat(0).take(s));
let size = if length <= s { length } else { s };
unsafe {
if length <= s {
ptr::copy( (&mut self.v[self.start..self.end]).as_ptr(), (&mut v[..length]).as_mut_ptr(), length);
self.v = v;
length
} else {
ptr::copy( (&mut self.v[self.start..(self.start+s)]).as_ptr(), (&mut v[..]).as_mut_ptr(), s);
self.v = v;
s
}
let slice_ptr = (&self.v[self.start..self.end]).as_ptr();
ptr::copy(slice_ptr, v.as_mut_ptr(), size);
}
self.v = v;
self.start = 0;
self.end = size;
size
}
}