如何将数据从一个切片写入同一个切片?
How can I write data from a slice to the same slice?
我想将一个切片的结尾写到同一个切片的顶部。
let mut foo = [1, 2, 3, 4, 5];
foo[..2].copy_from_slice(&[4..]); // error: multiple references to same data (mut and not)
assert!(foo, [4, 5, 3, 4, 5]);
我看过
我想要最大可能的性能(例如,通过使用 foo.as_ptr()
)。
通常要将数据从切片内的一个范围复制到另一个范围(允许重叠),我们甚至不能使用 .split_at_mut()
。
否则我会主要使用 .split_at_mut()
。 (有什么让你认为边界检查不会被优化的吗?另外,你是否复制了足够多的数据,相比之下它的影响很小?)
无论如何,这就是您可以将 std::ptr::copy
(允许重叠复制,a.k.a 内存移动)包装在保险箱或 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);
}
我找到了一个更好的方法来做我想做的事情:
fn main() {
let mut v = [1, 2, 3, 4, 5, 6];
// scoped to restrict the lifetime of the borrows
{
let (left, right) = v.split_at_mut(3);
assert!(left == [1, 2, 3]);
assert!(right == [4, 5, 6]);
for (l, r) in left.iter_mut().zip(right) {
*l = *r;
}
}
assert!(v == [4, 5, 6, 4, 5, 6]);
}
如果您的类型实现 Copy
和 子切片不重叠:
fn main() {
let mut v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.split_at_mut(3);
// Perform further work to ensure slices are the same length, as needed
left.copy_from_slice(right);
}
assert!(v == [4, 5, 6, 4, 5, 6]);
}
Rust 1.37 (2019-08-15) adds the library function slice::copy_within
完全符合您的要求:
let mut foo = [1, 2, 3, 4, 5];
foo.copy_within(3 .. 5, 0); // <-- THIS
assert_eq!(foo, [4, 5, 3, 4, 5]);
我想将一个切片的结尾写到同一个切片的顶部。
let mut foo = [1, 2, 3, 4, 5];
foo[..2].copy_from_slice(&[4..]); // error: multiple references to same data (mut and not)
assert!(foo, [4, 5, 3, 4, 5]);
我看过
我想要最大可能的性能(例如,通过使用 foo.as_ptr()
)。
通常要将数据从切片内的一个范围复制到另一个范围(允许重叠),我们甚至不能使用 .split_at_mut()
。
否则我会主要使用 .split_at_mut()
。 (有什么让你认为边界检查不会被优化的吗?另外,你是否复制了足够多的数据,相比之下它的影响很小?)
无论如何,这就是您可以将 std::ptr::copy
(允许重叠复制,a.k.a 内存移动)包装在保险箱或 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);
}
我找到了一个更好的方法来做我想做的事情:
fn main() {
let mut v = [1, 2, 3, 4, 5, 6];
// scoped to restrict the lifetime of the borrows
{
let (left, right) = v.split_at_mut(3);
assert!(left == [1, 2, 3]);
assert!(right == [4, 5, 6]);
for (l, r) in left.iter_mut().zip(right) {
*l = *r;
}
}
assert!(v == [4, 5, 6, 4, 5, 6]);
}
如果您的类型实现 Copy
和 子切片不重叠:
fn main() {
let mut v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.split_at_mut(3);
// Perform further work to ensure slices are the same length, as needed
left.copy_from_slice(right);
}
assert!(v == [4, 5, 6, 4, 5, 6]);
}
Rust 1.37 (2019-08-15) adds the library function slice::copy_within
完全符合您的要求:
let mut foo = [1, 2, 3, 4, 5];
foo.copy_within(3 .. 5, 0); // <-- THIS
assert_eq!(foo, [4, 5, 3, 4, 5]);