From: Alexandre Courbot Date: Mon, 15 Dec 2025 05:49:09 +0000 (+0900) Subject: rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=209c70953aa3630eb25e93e95464306a41b16d12;p=thirdparty%2Fkernel%2Flinux.git 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 Reviewed-by: Gary Guo Signed-off-by: Alexandre Courbot Reviewed-by: Benno Lossin Link: https://patch.msgid.link/20251215-transmute_unit-v4-1-477d71ec7c23@nvidia.com Signed-off-by: Miguel Ojeda --- diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs index be5dbf3829e25..5711580c9f9b5 100644 --- a/rust/kernel/transmute.rs +++ b/rust/kernel/transmute.rs @@ -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. + (), + {} core::marker::PhantomData, + // 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. + (), + {} core::marker::PhantomData, + // SAFETY: Instances of the following types have no uninitialized portions. u8, u16, u32, u64, usize, i8, i16, i32, i64, isize,