/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustix-1.0.7/src/utils.rs
Line | Count | Source |
1 | | //! Miscellaneous minor utilities. |
2 | | |
3 | | #![allow(dead_code)] |
4 | | #![allow(unused_macros)] |
5 | | |
6 | | use core::ffi::c_void; |
7 | | use core::mem::{align_of, size_of}; |
8 | | use core::ptr::{null, null_mut, NonNull}; |
9 | | |
10 | | /// Convert a `&T` into a `*const T` without using an `as`. |
11 | | #[inline] |
12 | 0 | pub(crate) const fn as_ptr<T>(t: &T) -> *const T { |
13 | 0 | t |
14 | 0 | } Unexecuted instantiation: rustix::utils::as_ptr::<rustix::event::epoll::Event> Unexecuted instantiation: rustix::utils::as_ptr::<[i8; 108]> Unexecuted instantiation: rustix::utils::as_ptr::<core::mem::maybe_uninit::MaybeUninit<libc::unix::linux_like::sockaddr_storage>> Unexecuted instantiation: rustix::utils::as_ptr::<rustix::timespec::Timespec> Unexecuted instantiation: rustix::utils::as_ptr::<rustix::fs::fd::Timestamps> Unexecuted instantiation: rustix::utils::as_ptr::<libc::unix::linux_like::sockaddr_un> Unexecuted instantiation: rustix::utils::as_ptr::<libc::unix::linux_like::sockaddr_storage> |
15 | | |
16 | | /// Convert a `&mut T` into a `*mut T` without using an `as`. |
17 | | #[inline] |
18 | 0 | pub(crate) fn as_mut_ptr<T>(t: &mut T) -> *mut T { |
19 | 0 | t |
20 | 0 | } Unexecuted instantiation: rustix::utils::as_mut_ptr::<libc::unix::linux_like::sockaddr_storage> Unexecuted instantiation: rustix::utils::as_mut_ptr::<u64> |
21 | | |
22 | | /// Convert an `Option<&T>` into a possibly-null `*const T`. |
23 | | #[inline] |
24 | 0 | pub(crate) const fn option_as_ptr<T>(t: Option<&T>) -> *const T { |
25 | 0 | match t { |
26 | 0 | Some(t) => t, |
27 | 0 | None => null(), |
28 | | } |
29 | 0 | } |
30 | | |
31 | | /// Convert an `Option<&mut T>` into a possibly-null `*mut T`. |
32 | | #[inline] |
33 | 0 | pub(crate) fn option_as_mut_ptr<T>(t: Option<&mut T>) -> *mut T { |
34 | 0 | match t { |
35 | 0 | Some(t) => t, |
36 | 0 | None => null_mut(), |
37 | | } |
38 | 0 | } |
39 | | |
40 | | /// Convert a `*mut c_void` to a `*mut T`, checking that it is not null, |
41 | | /// misaligned, or pointing to a region of memory that wraps around the address |
42 | | /// space. |
43 | 0 | pub(crate) fn check_raw_pointer<T>(value: *mut c_void) -> Option<NonNull<T>> { |
44 | 0 | if (value as usize).checked_add(size_of::<T>()).is_none() |
45 | 0 | || (value as usize) % align_of::<T>() != 0 |
46 | | { |
47 | 0 | return None; |
48 | 0 | } |
49 | | |
50 | 0 | NonNull::new(value.cast()) |
51 | 0 | } |
52 | | |
53 | | /// Create a union value containing a default value in one of its arms. |
54 | | /// |
55 | | /// The field names a union field which must have the same size as the union |
56 | | /// itself. |
57 | | macro_rules! default_union { |
58 | | ($union:ident, $field:ident) => {{ |
59 | | let u = $union { |
60 | | $field: Default::default(), |
61 | | }; |
62 | | |
63 | | // Assert that the given field initializes the whole union. |
64 | | #[cfg(test)] |
65 | | unsafe { |
66 | | let field_value = u.$field; |
67 | | assert_eq!( |
68 | | core::mem::size_of_val(&u), |
69 | | core::mem::size_of_val(&field_value) |
70 | | ); |
71 | | const_assert_eq!(memoffset::offset_of_union!($union, $field), 0); |
72 | | } |
73 | | |
74 | | u |
75 | | }}; |
76 | | } |