]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
authorAlexandre Courbot <acourbot@nvidia.com>
Mon, 15 Dec 2025 05:49:09 +0000 (14:49 +0900)
committerMiguel Ojeda <ojeda@kernel.org>
Mon, 26 Jan 2026 02:16:31 +0000 (03:16 +0100)
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>
rust/kernel/transmute.rs

index be5dbf3829e25c92f85083cc0f4940f35bb7baec..5711580c9f9b58beba28210488cf1b045fab7312 100644 (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,