]> git.proxmox.com Git - rustc.git/blob - vendor/rustix-0.36.5/src/utils.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / vendor / rustix-0.36.5 / src / utils.rs
1 /// Convert a `&T` into a `*const T` without using an `as`.
2 #[inline]
3 #[allow(dead_code)]
4 pub(crate) const fn as_ptr<T>(t: &T) -> *const T {
5 t
6 }
7
8 /// Convert a `&mut T` into a `*mut T` without using an `as`.
9 #[inline]
10 #[allow(dead_code)]
11 pub(crate) fn as_mut_ptr<T>(t: &mut T) -> *mut T {
12 t
13 }
14
15 /// Convert a `*mut c_void` to a `*mut T`, checking that it is not null,
16 /// misaligned, or pointing to a region of memory that wraps around the address
17 /// space.
18 #[allow(dead_code)]
19 pub(crate) fn check_raw_pointer<T>(value: *mut core::ffi::c_void) -> Option<core::ptr::NonNull<T>> {
20 if (value as usize)
21 .checked_add(core::mem::size_of::<T>())
22 .is_none()
23 || (value as usize) % core::mem::align_of::<T>() != 0
24 {
25 return None;
26 }
27
28 core::ptr::NonNull::new(value.cast())
29 }