一般来说,要将切片中的一个范围内的数据复制到另一个范围(允许重叠),我们甚至不能使用.split_at_mut()。
我会主要使用.split_at_mut()。 (有没有什么让你认为边界检查不会被优化出来?另外,你复制的数据是否足够多,相比之下它的影响很小?)
无论如何,这就是您可以将std::ptr::copy(允许重叠的复制,又名 memmove)包装在保险箱或 unsafe 函数中的方式。
use std::ptr::copy;
use std::ops::Range;
/// Copy the range `data[from]` onto the index `to` and following
///
/// **Panics** if `from` or `to` is out of bounds
pub fn move_memory<T: Copy>(data: &mut [T], from: Range<usize>, to: usize) {
assert!(from.start <= from.end);
assert!(from.end <= data.len());
assert!(to <= data.len() - (from.end - from.start));
unsafe {
move_memory_unchecked(data, from, to);
}
}
pub unsafe fn move_memory_unchecked<T: Copy>(data: &mut [T], from: Range<usize>, to: usize) {
debug_assert!(from.start <= from.end);
debug_assert!(from.end <= data.len());
debug_assert!(to <= data.len() - (from.end - from.start));
let ptr = data.as_mut_ptr();
copy(ptr.offset(from.start as isize),
ptr.offset(to as isize),
from.end - from.start)
}
fn main() {
let mut data = [0, 1, 2, 3, 4, 5, 6, 7];
move_memory(&mut data, 2..6, 0);
println!("{:?}", data);
move_memory(&mut data, 0..3, 5);
println!("{:?}", data);
}
Playground link