xref: /third_party/rust/crates/rustix/src/utils.rs
  • Home
  • History
  • Annotate Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/rust/crates/rustix/src/
1b8a62b91Sopenharmony_ci/// Convert a `&T` into a `*const T` without using an `as`.
2b8a62b91Sopenharmony_ci#[inline]
3b8a62b91Sopenharmony_ci#[allow(dead_code)]
4b8a62b91Sopenharmony_cipub(crate) const fn as_ptr<T>(t: &T) -> *const T {
5b8a62b91Sopenharmony_ci    t
6b8a62b91Sopenharmony_ci}
7b8a62b91Sopenharmony_ci
8b8a62b91Sopenharmony_ci/// Convert a `&mut T` into a `*mut T` without using an `as`.
9b8a62b91Sopenharmony_ci#[inline]
10b8a62b91Sopenharmony_ci#[allow(dead_code)]
11b8a62b91Sopenharmony_cipub(crate) fn as_mut_ptr<T>(t: &mut T) -> *mut T {
12b8a62b91Sopenharmony_ci    t
13b8a62b91Sopenharmony_ci}
14b8a62b91Sopenharmony_ci
15b8a62b91Sopenharmony_ci/// Convert a `*mut c_void` to a `*mut T`, checking that it is not null,
16b8a62b91Sopenharmony_ci/// misaligned, or pointing to a region of memory that wraps around the address
17b8a62b91Sopenharmony_ci/// space.
18b8a62b91Sopenharmony_ci#[allow(dead_code)]
19b8a62b91Sopenharmony_cipub(crate) fn check_raw_pointer<T>(value: *mut core::ffi::c_void) -> Option<core::ptr::NonNull<T>> {
20b8a62b91Sopenharmony_ci    if (value as usize)
21b8a62b91Sopenharmony_ci        .checked_add(core::mem::size_of::<T>())
22b8a62b91Sopenharmony_ci        .is_none()
23b8a62b91Sopenharmony_ci        || (value as usize) % core::mem::align_of::<T>() != 0
24b8a62b91Sopenharmony_ci    {
25b8a62b91Sopenharmony_ci        return None;
26b8a62b91Sopenharmony_ci    }
27b8a62b91Sopenharmony_ci
28b8a62b91Sopenharmony_ci    core::ptr::NonNull::new(value.cast())
29b8a62b91Sopenharmony_ci}
30

Indexes created Thu Nov 07 10:32:03 CST 2024