libc/
types.rs

1//! Platform-agnostic support types.
2
3#[cfg(feature = "extra_traits")]
4use core::hash::Hash;
5use core::mem::MaybeUninit;
6
7use crate::prelude::*;
8
9/// A transparent wrapper over `MaybeUninit<T>` to represent uninitialized padding
10/// while providing `Default`.
11// This is restricted to `Copy` types since that's a loose indicator that zeros is actually
12// a valid bitpattern. There is no technical reason this is required, though, so it could be
13// lifted in the future if it becomes a problem.
14#[allow(unused)]
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> fmt::Debug for Padding<T> {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        // Taken frmo `MaybeUninit`'s debug implementation
28        // NB: there is no `.pad_fmt` so we can't use a simpler `format_args!("Padding<{..}>").
29        let full_name = core::any::type_name::<Self>();
30        let prefix_len = full_name.find("Padding").unwrap();
31        f.pad(&full_name[prefix_len..])
32    }
33}
34
35/// Do nothing when hashing to ignore the existence of padding fields.
36#[cfg(feature = "extra_traits")]
37impl<T: Copy> Hash for Padding<T> {
38    fn hash<H: hash::Hasher>(&self, _state: &mut H) {}
39}
40
41/// Padding fields are all equal, regardless of what is inside them, so they do not affect anything.
42#[cfg(feature = "extra_traits")]
43impl<T: Copy> PartialEq for Padding<T> {
44    fn eq(&self, _other: &Self) -> bool {
45        true
46    }
47}
48
49/// Mark that `Padding` implements `Eq` so that it can be used in types that implement it.
50#[cfg(feature = "extra_traits")]
51impl<T: Copy> Eq for Padding<T> {}
52
53/// The default repr type used for C style enums in Rust.
54#[cfg(target_env = "msvc")]
55#[allow(unused)]
56pub(crate) type CEnumRepr = c_int;
57#[cfg(not(target_env = "msvc"))]
58#[allow(unused)]
59pub(crate) type CEnumRepr = c_uint;