Function aligned_ptr::ptr::try_write_bytes[][src]

pub unsafe fn try_write_bytes<T>(
    dst: *mut T,
    val: u8,
    count: usize
) -> Result<()>
Expand description

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

Safety

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

Errors

This function returns an error:

Examples

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

let mut slice = [0_u8; 4];

let r = unsafe { ptr::try_write_bytes(slice.as_mut_ptr(), 0xff, 4) };
assert!(r.is_ok());
assert_eq!(slice, [0xff, 0xff, 0xff, 0xff]);

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

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