Function aligned_ptr::ptr::try_get[][src]

pub unsafe fn try_get<T: Copy>(p: *const T) -> Result<T>
Expand description

The wrapper of *p which returns an error if the pointer 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 b = Box::new(3);
let p = Box::into_raw(b);
assert_eq!(unsafe { ptr::try_get(p) }, Ok(3));

let p: *const i32 = core::ptr::null();
assert_eq!(unsafe { ptr::try_get(p) }, Err(Error::Null));

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