Safe version of `ptr::copy` as `slice::copy`


#1

It would be handful to have safe version of ptr::copy. I think that this one:

fn copy<T: Copy>(src: &[T], dst: &mut [T]) {
    assert!(src.len() <= dst.len());
    unsafe { ptr::copy(src.as_ptr(), dst.as_mut_ptr(), src.len()); }
}

Would be perfectly safe and handful.

PS:

I know that we can use loop but sometimes loop wasn’t be optimised out. Also I think that slice::copy(a, b) is more straightforward than:

for (src, dst) in src.iter().zip(&mut dst) {
    *dst = *src
}

#2

The discussion is ongoing in this issue Tracking issue for clone_from_slice stabilization


#3

See the thread I started on the same issue for the same reason: Stabilizing basic functions on arrays and slices


#4

fwiw, I like the free function idea. Either with Clone or with Copy bound. Copy is nice because then then name is simple to decide… :smile: