libc/
types.rs

1//! Platform-agnostic support types.
2
3use core::mem::MaybeUninit;
4
5use crate::prelude::*;
6
7/// A transparent wrapper over `MaybeUninit<T>` to represent uninitialized padding
8/// while providing `Default`.
9// This is restricted to `Copy` types since that's a loose indicator that zeros is actually
10// a valid bitpattern. There is no technical reason this is required, though, so it could be
11// lifted in the future if it becomes a problem.
12#[allow(unused)]
13#[repr(transparent)]
14#[derive(Clone, Copy)]
15pub(crate) struct Padding<T: Copy>(MaybeUninit<T>);
16
17impl<T: Copy> Default for Padding<T> {
18    fn default() -> Self {
19        Self(MaybeUninit::zeroed())
20    }
21}
22
23impl<T: Copy> fmt::Debug for Padding<T> {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        // Taken frmo `MaybeUninit`'s debug implementation
26        // NB: there is no `.pad_fmt` so we can't use a simpler `format_args!("Padding<{..}>").
27        let full_name = core::any::type_name::<Self>();
28        let prefix_len = full_name.find("Padding").unwrap();
29        f.pad(&full_name[prefix_len..])
30    }
31}
32
33/// The default repr type used for C style enums in Rust.
34#[cfg(target_env = "msvc")]
35#[allow(unused)]
36pub(crate) type CEnumRepr = c_int;
37#[cfg(not(target_env = "msvc"))]
38#[allow(unused)]
39pub(crate) type CEnumRepr = c_uint;