rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs

This is useful when using types that may or may not be empty in generic
code relying on these traits. It is also safe because technically a
no-op.

Reviewed-by: Alistair Popple <apopple@nvidia.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Link: https://patch.msgid.link/20251215-transmute_unit-v4-1-477d71ec7c23@nvidia.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
Alexandre Courbot 2025-12-15 14:49:09 +09:00 committed by Miguel Ojeda
parent 7f87c7a003
commit 209c70953a

View file

@ -170,6 +170,10 @@ macro_rules! impl_frombytes {
}
impl_frombytes! {
// SAFETY: Inhabited ZSTs only have one possible bit pattern, and these two have no invariant.
(),
{<T>} core::marker::PhantomData<T>,
// SAFETY: All bit patterns are acceptable values of the types below.
u8, u16, u32, u64, usize,
i8, i16, i32, i64, isize,
@ -230,6 +234,10 @@ macro_rules! impl_asbytes {
}
impl_asbytes! {
// SAFETY: Inhabited ZSTs only have one possible bit pattern, and these two have no invariant.
(),
{<T>} core::marker::PhantomData<T>,
// SAFETY: Instances of the following types have no uninitialized portions.
u8, u16, u32, u64, usize,
i8, i16, i32, i64, isize,