Function aligned_ptr::slice::try_from_raw_parts_mut[][src]

pub unsafe fn try_from_raw_parts_mut<'a, T>(
    data: *mut T,
    len: usize
) -> Result<&'a mut [T]>
Expand description

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

Safety

The caller must follow the rules required by core::slice::from_raw_parts_mut except the alignment and null rules.

Errors

This function may return an error:

Examples

use aligned_ptr::slice;
use aligned_ptr::Error;

let mut x = 3;
let s = unsafe { slice::try_from_raw_parts_mut(&mut x, 1) };

assert!(s.is_ok());
assert_eq!(s.unwrap(), [3]);

let p: *mut i32 = core::ptr::null_mut();
let s = unsafe { slice::try_from_raw_parts_mut(p, 1) };
assert_eq!(s, Err(Error::Null));

let p = 0x1001 as *mut i32;
let s = unsafe { slice::try_from_raw_parts_mut(p, 1) };
assert_eq!(s, Err(Error::NotAligned));