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(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    /// Create a `Padding` initialized with the given value.
27    #[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        // Taken frmo `MaybeUninit`'s debug implementation
36        // NB: there is no `.pad_fmt` so we can't use a simpler `format_args!("Padding<{..}>").
37        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/// Do nothing when hashing to ignore the existence of padding fields.
44#[cfg(feature = "extra_traits")]
45impl<T: Copy> Hash for Padding<T> {
46    fn hash<H: hash::Hasher>(&self, _state: &mut H) {}
47}
48
49/// Padding fields are all equal, regardless of what is inside them, so they do not affect anything.
50#[cfg(feature = "extra_traits")]
51impl<T: Copy> PartialEq for Padding<T> {
52    fn eq(&self, _other: &Self) -> bool {
53        true
54    }
55}
56
57/// Mark that `Padding` implements `Eq` so that it can be used in types that implement it.
58#[cfg(feature = "extra_traits")]
59impl<T: Copy> Eq for Padding<T> {}
60
61/// The default repr type used for C style enums in Rust.
62#[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;