1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
use core::{
fmt::{self, Debug},
marker::PhantomData,
};
use bytemuck::Zeroable;
use crate::TypeSize;
/// Constructs an [`IsZeroable<$T>`](struct@crate::IsZeroable),
/// requires `$T:`[`Zeroable`].
///
/// This has an optional type argument (`$T`) that defaults to
/// infering the type if not passed.
///
/// This macro is defined for completeness' sake,
/// no function in this crate takes `IsZeroable` by itself,
/// always a [`TypeSize<T, IsZeroable<T>, _>`](struct@crate::TypeSize),
/// which can be constructed with the
/// [`TypeSize`](macro@crate::TypeSize) macro.
///
/// Related: the [`copying`](crate::copying) module
///
/// # Example
///
/// ```rust
/// use constmuck::{IsZeroable, TypeSize};
///
/// const FOO: IsZeroable<u32> = IsZeroable!();
/// assert_eq!(constmuck::zeroed(TypeSize!(u32).with_bounds(FOO)), 0u32);
/// // alternatively, the typical way to call `constmuck::zeroed`.
/// assert_eq!(constmuck::zeroed(TypeSize!(u32)), 0u32);
///
///
/// const BAR: IsZeroable<u8> = IsZeroable!(u8);
/// assert_eq!(constmuck::zeroed_array(TypeSize!(u8).with_bounds(BAR)), [0u8; 4]);
/// // alternatively, the typical way to call `constmuck::zeroed_array`.
/// assert_eq!(constmuck::zeroed_array(TypeSize!(u8)), [0u8; 4]);
///
/// ```
///
/// [`Zeroable`]: trait@Zeroable
#[macro_export]
macro_rules! IsZeroable {
() => {
<$crate::IsZeroable<_> as $crate::Infer>::INFER
};
($T:ty) => {
<$crate::IsZeroable<$T> as $crate::Infer>::INFER
};
}
mod __ {
use super::*;
/// Encodes a `T:`[`Zeroable`] bound as a value.
///
/// Related: the [`zeroed`] and [`zeroed_array`] functions.
///
/// [`Zeroable`]: trait@Zeroable
pub struct IsZeroable<T> {
// The lifetime of `T` is invariant,
// just in case that it's unsound for lifetimes to be co/contravariant.
_private: PhantomData<fn(T) -> T>,
}
impl<T> Debug for IsZeroable<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("IsZeroable")
}
}
impl<T> Copy for IsZeroable<T> {}
impl<T> Clone for IsZeroable<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: Zeroable> IsZeroable<T> {
/// Constructs an `IsZeroable`
///
/// You can also use the [`IsZeroable`](macro@crate::IsZeroable)
/// macro to construct `IsZeroable` arguments.
pub const NEW: Self = Self {
_private: PhantomData,
};
}
impl<T> IsZeroable<T> {
const __NEW_UNCHECKED__: Self = Self {
_private: PhantomData,
};
/// Constructs an `IsZeroable<T>` without checking that `T` implements [`Zeroable`].
///
/// # Safety
///
/// You must ensure that `T` follows the
/// [safety requirements of `Zeroable`](trait@bytemuck::Zeroable#safety)
///
/// [`Zeroable`]: trait@Zeroable
#[inline(always)]
pub const unsafe fn new_unchecked() -> Self {
Self::__NEW_UNCHECKED__
}
}
}
pub use __::IsZeroable;
impl<T: Zeroable> crate::Infer for IsZeroable<T> {
const INFER: Self = Self::NEW;
}
/// Constructs a zero-initialized `T`,
/// equivalent to [`std::mem::zeroed::<T>()`](core::mem::zeroed).
///
/// This function requires that `T` implements [`Zeroable`].
///
/// # Example
///
/// ```rust
/// use constmuck::{TypeSize, zeroed};
///
/// const BYTES: [u8; 4] = zeroed(TypeSize!([u8; 4]));
/// const CHARS: [char; 4] = zeroed(TypeSize!([char; 4]));
///
/// assert_eq!(BYTES, [0, 0, 0, 0]);
/// assert_eq!(CHARS, ['\0', '\0', '\0', '\0']);
///
/// ```
///
/// [`Zeroable`]: trait@Zeroable
pub const fn zeroed<T, const SIZE: usize>(_bounds: TypeSize<T, IsZeroable<T>, SIZE>) -> T {
// safety:
// `IsZeroable<T>` guarantees that `std::mem::zeroed::<T>` is sound to call.
//
// `TypeSize<T, _, SIZE>` guarantees that `T` is `SIZE` bytes large
//
unsafe { __priv_transmute!([u8; SIZE], T, [0; SIZE]) }
}
/// Constructs a zero-initialized `[T; N]`,
/// equivalent to [`std::mem::zeroed::<[T; N]>()`](core::mem::zeroed).
///
/// This function requires that `T` implements [`Zeroable`].
///
/// To specify the length of the returned array, [`TypeSize::zeroed_array`]
/// can be used instead.
///
/// # Example
///
/// ```rust
/// use constmuck::{TypeSize, zeroed_array};
///
/// const BYTES: [u8; 2] = zeroed_array(TypeSize!(u8));
/// assert_eq!(BYTES, [0, 0]);
/// // using `TypeSize::zeroed_array` to pass the length of the returned array.
/// assert_eq!(TypeSize!(u8).zeroed_array::<2>(), [0, 0]);
///
///
/// const CHARS: [char; 4] = zeroed_array(TypeSize!(char));
/// assert_eq!(CHARS, ['\0', '\0', '\0', '\0']);
/// // using `TypeSize::zeroed_array` to pass the length of the returned array.
/// assert_eq!(TypeSize!(char).zeroed_array::<4>(), ['\0', '\0', '\0', '\0']);
///
/// ```
///
/// [`Zeroable`]: trait@Zeroable
pub const fn zeroed_array<T, const SIZE: usize, const LEN: usize>(
_bounds: TypeSize<T, IsZeroable<T>, SIZE>,
) -> [T; LEN] {
if crate::__priv_utils::SizeIsStride::<T, LEN>::V {
crate::__priv_utils::SizeIsStride::<T, LEN>::panic();
}
// safety: see `zeroable::zeroed`
unsafe { __priv_transmute!([[u8; SIZE]; LEN], [T; LEN], [[0u8; SIZE]; LEN]) }
}