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
).
§Safety
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
.
Re-exports§
pub use error::Error;
Modules§
- error
- Errors.
- ptr
- A module containing functions defined in
core::ptr
with null and alignment checks. - slice
- A module containing functions defined in
core::slice
with null and alignment checks.
Type Aliases§
- Result
core::result::Result
which may containError
.