[PATCH] rust: transmute: implement FromBytes and AsBytes for ()

Alexandre Courbot posted 1 patch 1 week, 4 days ago
There is a newer version of this series
rust/kernel/transmute.rs | 6 ++++++
1 file changed, 6 insertions(+)
[PATCH] rust: transmute: implement FromBytes and AsBytes for ()
Posted by Alexandre Courbot 1 week, 4 days ago
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.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
This is going to be useful in Nova's GSP message handling, as some
messages are empty and we need to explicitly use an empty structure for
them.

If accepted, I would like to merge it through `drm-rust-next` so Nova
code can start using this feature quickly.
---
 rust/kernel/transmute.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index be5dbf3829e2..8d78c81e3749 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
 }
 
 impl_frombytes! {
+    // SAFETY: This type is empty and thus does not consume any data.
+    (),
+
     // SAFETY: All bit patterns are acceptable values of the types below.
     u8, u16, u32, u64, usize,
     i8, i16, i32, i64, isize,
@@ -230,6 +233,9 @@ macro_rules! impl_asbytes {
 }
 
 impl_asbytes! {
+    // SAFETY: This type is empty and thus returns an empty slice.
+    (),
+
     // SAFETY: Instances of the following types have no uninitialized portions.
     u8, u16, u32, u64, usize,
     i8, i16, i32, i64, isize,

---
base-commit: ba65a4e7120a616d9c592750d9147f6dcafedffa
change-id: 20251208-transmute_unit-78ab58ba9e6e

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH] rust: transmute: implement FromBytes and AsBytes for ()
Posted by Alice Ryhl 1 week, 1 day ago
On Mon, Dec 08, 2025 at 01:15:13PM +0900, Alexandre Courbot wrote:
> 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.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> This is going to be useful in Nova's GSP message handling, as some
> messages are empty and we need to explicitly use an empty structure for
> them.
> 
> If accepted, I would like to merge it through `drm-rust-next` so Nova
> code can start using this feature quickly.
> ---
>  rust/kernel/transmute.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index be5dbf3829e2..8d78c81e3749 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
>  }
>  
>  impl_frombytes! {
> +    // SAFETY: This type is empty and thus does not consume any data.
> +    (),

This should mention that the type has no invariants.

>  impl_asbytes! {
> +    // SAFETY: This type is empty and thus returns an empty slice.
> +    (),

Ok.

Alice