Closed
Description
Would you like to fix this issue? Here's how!
I'm dealing with a case where I have to send a byte array buffer to a destructive function multiple times. This function requires the buffer to start with an initialization sequence that I have to set.
I want to be able to efficiently reset the header bytes but the size of the initialization sequence is less than the total buffer and so I hit the destination and source slices have different lengths
error.
The real question is, why is this required? Here's the current code:
#[inline]
fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
assert!(self.len() == src.len(),
"destination and source slices have different lengths");
unsafe {
ptr::copy_nonoverlapping(
src.as_ptr(), self.as_mut_ptr(), self.len());
}
}
In theory, if self
is big enough to hold src
shouldn't that be enough? The adjusted call should be this then:
#[inline]
fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
assert!(self.len() >= src.len(),
"destination slice must be large enough to hold source");
unsafe {
ptr::copy_nonoverlapping(
src.as_ptr(), self.as_mut_ptr(), src.len());
}
}
I think the same should be done for clone_from_slice.