rustix/types.rs
1//! Platform-agnostic support types.
2//!
3//! Copied from https://github.com/rust-lang/libc/blob/008f905256d27eb5dd416d0976678ee6c2855554/src/types.rs
4
5use core::fmt;
6use core::mem::MaybeUninit;
7
8/// A transparent wrapper over `MaybeUninit<T>` to represent uninitialized padding
9/// while providing `Default`.
10// This is restricted to `Copy` types since that's a loose indicator that zeros is actually
11// a valid bitpattern. There is no technical reason this is required, though, so it could be
12// lifted in the future if it becomes a problem.
13#[allow(dead_code)]
14#[repr(transparent)]
15#[derive(Clone, Copy)]
16pub(crate) struct Padding<T: Copy>(MaybeUninit<T>);
17
18impl<T: Copy> Default for Padding<T> {
19 fn default() -> Self {
20 Self(MaybeUninit::zeroed())
21 }
22}
23
24impl<T: Copy> Padding<T> {
25 /// Create a `Padding` initialized with the given value.
26 #[allow(dead_code)]
27 pub(crate) const fn new(val: T) -> Self {
28 Self(MaybeUninit::new(val))
29 }
30}
31
32impl<T: Copy> fmt::Debug for Padding<T> {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 // Taken from `MaybeUninit`'s debug implementation
35 // NB: there is no `.pad_fmt` so we can't use a simpler `format_args!("Padding<{..}>").
36 let full_name = core::any::type_name::<Self>();
37 let prefix_len = full_name.find("Padding").unwrap();
38 f.pad(&full_name[prefix_len..])
39 }
40}