summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/rustix/src/utils.rs')
-rw-r--r--vendor/rustix/src/utils.rs25
1 files changed, 12 insertions, 13 deletions
diff --git a/vendor/rustix/src/utils.rs b/vendor/rustix/src/utils.rs
index d6ad43b54..df2ac315b 100644
--- a/vendor/rustix/src/utils.rs
+++ b/vendor/rustix/src/utils.rs
@@ -1,49 +1,48 @@
+#![allow(dead_code)]
+
+use core::ffi::c_void;
+use core::mem::{align_of, size_of};
+use core::ptr::{null, null_mut, NonNull};
+
/// Convert a `&T` into a `*const T` without using an `as`.
#[inline]
-#[allow(dead_code)]
pub(crate) const fn as_ptr<T>(t: &T) -> *const T {
t
}
/// Convert a `&mut T` into a `*mut T` without using an `as`.
#[inline]
-#[allow(dead_code)]
pub(crate) fn as_mut_ptr<T>(t: &mut T) -> *mut T {
t
}
/// Convert an `Option<&T>` into a possibly-null `*const T`.
#[inline]
-#[allow(dead_code)]
pub(crate) const fn optional_as_ptr<T>(t: Option<&T>) -> *const T {
match t {
Some(t) => t,
- None => core::ptr::null(),
+ None => null(),
}
}
/// Convert an `Option<&mut T>` into a possibly-null `*mut T`.
#[inline]
-#[allow(dead_code)]
pub(crate) fn optional_as_mut_ptr<T>(t: Option<&mut T>) -> *mut T {
match t {
Some(t) => t,
- None => core::ptr::null_mut(),
+ None => null_mut(),
}
}
/// Convert a `*mut c_void` to a `*mut T`, checking that it is not null,
/// misaligned, or pointing to a region of memory that wraps around the address
/// space.
-#[allow(dead_code)]
-pub(crate) fn check_raw_pointer<T>(value: *mut core::ffi::c_void) -> Option<core::ptr::NonNull<T>> {
- if (value as usize)
- .checked_add(core::mem::size_of::<T>())
- .is_none()
- || (value as usize) % core::mem::align_of::<T>() != 0
+pub(crate) fn check_raw_pointer<T>(value: *mut c_void) -> Option<NonNull<T>> {
+ if (value as usize).checked_add(size_of::<T>()).is_none()
+ || (value as usize) % align_of::<T>() != 0
{
return None;
}
- core::ptr::NonNull::new(value.cast())
+ NonNull::new(value.cast())
}