1#[cfg(feature = "extra_traits")]
4use core::hash::Hash;
5use core::mem::MaybeUninit;
6
7use crate::prelude::*;
8
9#[allow(dead_code)]
15#[repr(transparent)]
16#[derive(Clone, Copy)]
17pub(crate) struct Padding<T: Copy>(MaybeUninit<T>);
18
19impl<T: Copy> Default for Padding<T> {
20 fn default() -> Self {
21 Self(MaybeUninit::zeroed())
22 }
23}
24
25impl<T: Copy> Padding<T> {
26 #[allow(dead_code)]
28 pub(crate) const fn new(val: T) -> Self {
29 Self(MaybeUninit::new(val))
30 }
31}
32
33impl<T: Copy> fmt::Debug for Padding<T> {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 let full_name = core::any::type_name::<Self>();
38 let prefix_len = full_name.find("Padding").unwrap();
39 f.pad(&full_name[prefix_len..])
40 }
41}
42
43#[cfg(feature = "extra_traits")]
45impl<T: Copy> Hash for Padding<T> {
46 fn hash<H: hash::Hasher>(&self, _state: &mut H) {}
47}
48
49#[cfg(feature = "extra_traits")]
51impl<T: Copy> PartialEq for Padding<T> {
52 fn eq(&self, _other: &Self) -> bool {
53 true
54 }
55}
56
57#[cfg(feature = "extra_traits")]
59impl<T: Copy> Eq for Padding<T> {}
60
61#[cfg(target_env = "msvc")]
63#[allow(unused)]
64pub(crate) type CEnumRepr = c_int;
65#[cfg(not(target_env = "msvc"))]
66#[allow(unused)]
67pub(crate) type CEnumRepr = c_uint;
68
69#[allow(unused)]
72pub(crate) const fn u16_cast_short(x: u16) -> c_short {
73 assert!(size_of::<u16>() <= size_of::<c_short>()); x as i16
75}
76
77#[allow(unused)]
80pub(crate) const fn u32_cast_int(x: u32) -> c_int {
81 assert!(size_of::<u32>() <= size_of::<c_int>());
83 x as i32
84}
85
86#[allow(unused)]
89pub(crate) const fn u32_cast_long(x: u32) -> c_long {
90 assert!(size_of::<u32>() <= size_of::<c_long>()); x as c_long
92}
93
94#[allow(unused)]
96pub(crate) const fn ulong_cast_int(x: c_ulong) -> c_int {
97 assert!(x <= (c_int::MAX as c_ulong));
98 x as c_int
99}
100
101#[allow(unused)]
103pub(crate) const fn ulong_cast_uint(x: c_ulong) -> c_uint {
104 assert!(x <= (c_uint::MAX as c_ulong));
105 x as c_uint
106}
107
108#[allow(unused)]
111#[cfg(any(target_os = "linux", target_os = "android", target_os = "l4re"))]
112pub(crate) const fn u32_cast_ioctl(x: u32) -> crate::Ioctl {
113 assert!(size_of::<u32>() <= size_of::<crate::Ioctl>()); x as crate::Ioctl
115}
116
117#[allow(unused)]
118pub(crate) const fn u8_slice_cast_char_slice(x: &[u8]) -> &[c_char] {
119 assert!(size_of::<u8>() == size_of::<c_char>());
120 unsafe { mem::transmute::<&[u8], &[c_char]>(x) }
122}
123
124#[must_use]
128#[allow(dead_code)]
129pub const fn replace_array_items<T: Copy, const N: usize>(
130 mut dst: [T; N],
131 src: &[T],
132 start: usize,
133) -> [T; N] {
134 let mut i = 0;
135 while i < src.len() {
136 dst[i + start] = src[i];
137 i += 1;
138 }
139 dst
140}