Function std::ptr::copy_nonoverlapping_memory[src]

pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *T, count: uint)

Copies data from one location to another.

Copies count elements (not bytes) from src to dst. The source and destination may not overlap.

copy_nonoverlapping_memory is semantically equivalent to C's memcpy.

Example

A safe swap function:

use std::cast;
use std::mem;
use std::ptr;

fn swap<T>(x: &mut T, y: &mut T) {
    unsafe {
        // Give ourselves some scratch space to work with
        let mut t: T = mem::uninit();

        // Perform the swap, `&mut` pointers never alias
        ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
        ptr::copy_nonoverlapping_memory(x, &*y, 1);
        ptr::copy_nonoverlapping_memory(y, &t, 1);

        // y and t now point to the same thing, but we need to completely forget `tmp`
        // because it's no longer relevant.
        cast::forget(t);
    }
}

Safety Note

If the source and destination overlap then the behavior of this function is undefined.