Function aligned_ptr::ptr::try_as_mut[][src]

pub unsafe fn try_as_mut<'a, T>(p: *mut T) -> Result<&'a mut T>
Expand description

The wrapper of &mut *p which may return an error if p is either null or not aligned.

Safety

The caller must follow the safety rules listed at core::ptr except the alignment and null rules.

Errors

This function may return an error:

Examples

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

let mut x = 3;
let p = &mut x as *mut i32;
let r = unsafe { ptr::try_as_mut(p) };

if let Ok(r) = r {
    *r = 4;
    assert_eq!(x, 4);
} else {
    unreachable!();
}

let mut p: *mut i32 = core::ptr::null_mut();
let r = unsafe { ptr::try_as_mut(p) };
assert_eq!(r, Err(Error::Null));

let mut p = 0x1001 as *mut i32;
let r = unsafe { ptr::try_as_mut(p) };
assert_eq!(r, Err(Error::NotAligned));