Crate aligned_ptr[][src]

Expand description

A library to ensure that a pointer is aligned and not null when it dereferences.

This crate contains wrappers of unsafe functions defined in core::ptr and core::slice. These wrappers panic or return errors if the passed pointers are null or not aligned correctly.

For example, the code below panics because p is not aligned correctly.

use aligned_ptr::ptr;

let x = 0xdead_beaf_u32;
let p = (&x as *const u32 as usize + 1) as *const u16;

unsafe { ptr::read(p) };

If we import core::ptr instead of ptr, the code may run successfully. However, dereferencing unaligned pointer causes undefined behaviors and must be avoided by all means (except core::ptr::read_unaligned and core::ptr::write_unaligned).


While the functions defined in this crate rejects unaligned or null pointers, the caller must follow the other safety rules. For more information, see the safety note of core::ptr.


pub use error::Error;





A module containing functions defined in core::ptr with null and alignment checks.


A module containing functions defined in core::slice with null and alignment checks.

Type Definitions


core::result::Result which may contain Error.