Function copy_nonoverlapping
unsafe const fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize)
Copies count * size_of::<T>() bytes from src to dst. The source
and destination must not overlap.
For regions of memory which might overlap, use copy instead.
copy_nonoverlapping is semantically equivalent to C's memcpy, but
with the source and destination arguments swapped,
and count counting the number of Ts instead of bytes.
The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
requirements of T. The initialization state is preserved exactly.
Safety
Behavior is undefined if any of the following conditions are violated:
-
srcmust be valid for reads ofcount * size_of::<T>()bytes. -
dstmust be valid for writes ofcount * size_of::<T>()bytes. -
Both
srcanddstmust be properly aligned. -
The region of memory beginning at
srcwith a size ofcount * size_of::<T>()bytes must not overlap with the region of memory beginning atdstwith the same size.
Like read, copy_nonoverlapping 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.
Note that even if the effectively copied size (count * size_of::<T>()) is
0, the pointers must be properly aligned.
Examples
Manually implement Vec::append:
use ptr;
/// Moves all the elements of `src` into `dst`, leaving `src` empty.
let mut a = vec!;
let mut b = vec!;
append;
assert_eq!;
assert!;