diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:24 +0000 |
commit | 023939b627b7dc93b01471f7d41fb8553ddb4ffa (patch) | |
tree | 60fc59477c605c72b0a1051409062ddecc43f877 /vendor/rustix-0.37.22/src/utils.rs | |
parent | Adding debian version 1.72.1+dfsg1-1. (diff) | |
download | rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.tar.xz rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.zip |
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix-0.37.22/src/utils.rs')
-rw-r--r-- | vendor/rustix-0.37.22/src/utils.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/vendor/rustix-0.37.22/src/utils.rs b/vendor/rustix-0.37.22/src/utils.rs new file mode 100644 index 000000000..df2ac315b --- /dev/null +++ b/vendor/rustix-0.37.22/src/utils.rs @@ -0,0 +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] +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] +pub(crate) fn as_mut_ptr<T>(t: &mut T) -> *mut T { + t +} + +/// Convert an `Option<&T>` into a possibly-null `*const T`. +#[inline] +pub(crate) const fn optional_as_ptr<T>(t: Option<&T>) -> *const T { + match t { + Some(t) => t, + None => null(), + } +} + +/// Convert an `Option<&mut T>` into a possibly-null `*mut T`. +#[inline] +pub(crate) fn optional_as_mut_ptr<T>(t: Option<&mut T>) -> *mut T { + match t { + Some(t) => t, + 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. +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; + } + + NonNull::new(value.cast()) +} |