Function std::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:
-
Both
src
anddst
must be valid. -
Both
src
anddst
must be properly aligned. -
src.offset(count)
must be valid. In other words, the region of memory which begins atsrc
and has a length ofcount * size_of::<T>()
bytes must belong to a single, live allocation. -
dst.offset(count)
must be valid. In other words, the region of memory which begins atdst
and has a length ofcount * size_of::<T>()
bytes must belong to a single, live allocation.
Like read
, copy
creates a bitwise copy of T
, regardless of
whether T
is Copy
. If T
is not Copy
, using both the values
in the region beginning at *src
and the region beginning at *dst
can
violate memory safety.
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