Function aligned_ptr::ptr::try_swap_nonoverlapping[][src]

pub unsafe fn try_swap_nonoverlapping<T>(
    x: *mut T,
    y: *mut T,
    count: usize
) -> Result<()>
Expand description

The wrapper of core::ptr::swap_nonoverlapping which returns an error unless the passed pointers are aligned and not null.

Safety

The caller must follow the safety rules required by core::ptr::swap_nonoverlapping excpet the alignment and null rules.

Errors

This function may return an error:

Examples

use aligned_ptr::ptr;
use aligned_ptr::Error;

let mut x = [1, 2, 3];
let mut y = [4, 5, 6];

let r = unsafe { ptr::try_swap_nonoverlapping(x.as_mut_ptr(), y.as_mut_ptr(), 3) };
assert!(r.is_ok());
assert_eq!(x, [4, 5, 6]);
assert_eq!(y, [1, 2, 3]);

let z: *mut i32 = core::ptr::null_mut();
let r = unsafe { ptr::try_swap_nonoverlapping(x.as_mut_ptr(), z, 3) };
assert_eq!(r, Err(Error::Null));

let z = 0x1001 as *mut i32;
let r = unsafe { ptr::try_swap_nonoverlapping(x.as_mut_ptr(), z, 3) };
assert_eq!(r, Err(Error::NotAligned));