Function aligned_ptr::ptr::try_read_volatile[][src]

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

The wrapper of core::ptr::read_volatile which returns an error if the passed pointer is either null or not aligned.

Safety

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

Errors

This function may return an error:

Examples

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

let x = 3;
let p = &x as *const _;

assert_eq!(unsafe { ptr::try_read_volatile(p) }, Ok(3));

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

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