Function core::intrinsics::copy 1.0.0[−][src]
pub unsafe extern "rust-intrinsic" fn copy<T>(
src: *const T,
dst: *mut T,
count: usize
)
Copies count * size_of::<T>() bytes from src to dst. The source
and destination may overlap.
If the source and destination will never overlap,
copy_nonoverlapping can be used instead.
copy is semantically equivalent to C's memmove.
Safety
Behavior is undefined if any of the following conditions are violated:
-
The region of memory which begins at
srcand has a length ofcount * size_of::<T>()bytes must be both valid and initialized. -
The region of memory which begins at
dstand has a length ofcount * size_of::<T>()bytes must be valid (but may or may not be initialized). -
srcmust be properly aligned. -
dstmust be properly aligned.
Additionally, if T is not Copy, only the region at src or the
region at dst can be used or dropped after calling copy. copy
creates bitwise copies of T, regardless of whether T: Copy, which
can result in undefined behavior if both copies are used.
Examples
Efficiently create a Rust vector from an unsafe buffer:
use std::ptr; unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> { let mut dst = Vec::with_capacity(elts); dst.set_len(elts); ptr::copy(ptr, dst.as_mut_ptr(), elts); dst }Run