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 /// 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 { pub fn resize(&mut self, s: usize) -> usize {
let mut v = Vec::with_capacity(s); let mut v = Vec::with_capacity(s);
v.extend(repeat(0).take(s));
let length = self.end - self.start; let length = self.end - self.start;
v.extend(repeat(0).take(s));
let size = if length <= s { length } else { s };
unsafe { unsafe {
if length <= s { let slice_ptr = (&self.v[self.start..self.end]).as_ptr();
ptr::copy( (&mut self.v[self.start..self.end]).as_ptr(), (&mut v[..length]).as_mut_ptr(), length); ptr::copy(slice_ptr, v.as_mut_ptr(), size);
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
}
} }
self.v = v;
self.start = 0;
self.end = size;
size
} }
} }