Function aligned_ptr::ptr::try_write[][src]

pub unsafe fn try_write<T>(p: *mut T, v: T) -> Result<()>
Expand description

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

Safety

The caller must follow the safety rules required by core::ptr::write 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_write(p, 4) };
assert!(r.is_ok());
assert_eq!(x, 4);

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

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