Skip to contentSkip to conversation

rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs

PATCH Applied Alexandre Courbot 5 participants 13 messages Dec 15, 2025 – Jan 26

Subsystems (4)
lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

v4 cover rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs

This is useful in Nova's GSP message handling, as some messages are empty and we currently 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.

---
Changes in v4:
- Improve safety comment to mention lack of invariants.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Use better safety statement. (thanks Gary!)
- Also add PhantomData. (thanks Gary!)
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Make use of new feature in Nova.
- Link to v1: https://patch.msgid.link/[email protected]

---
Alexandre Courbot (2):
rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
gpu: nova-core: gsp: use () as message type for GspInitDone message

 drivers/gpu/nova-core/gsp/commands.rs | 6 +++---
 rust/kernel/transmute.rs              | 8 ++++++++
 2 files changed, 11 insertions(+), 3 deletions(-)

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20251208-transmute_unit-78ab58ba9e6e

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
author maintainer
#1 lore raw

PATCH v4 1/2 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.

---
 rust/kernel/transmute.rs | 8 ++++++++
 1 file changed, 8 insertions(+)
rust/kernel/transmute.rs +8 -0
index be5dbf3829e2..5711580c9f9b 100644
@@ -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,
Signature · 1 line
2.52.0
reviewer Reviewed-by on v4 1/2
#2 lore raw
Quoted 7 lines · Alexandre Courbot

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.

Cheers,
Benno

Quoted 3 lines
---
 rust/kernel/transmute.rs | 8 ++++++++
 1 file changed, 8 insertions(+)
author maintainer
#3 lore raw
Quoted 10 lines · Benno Lossin
Quoted 7 lines · Alexandre Courbot

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.

Miguel, are you ok if we take this one through the drm-rust tree? Nova is likely to make use of it this cycle.

Acked-by
#4 lore raw
Quoted 3 lines · Alexandre Courbot

Miguel, are you ok if we take this one through the drm-rust tree? Nova is likely to make use of it this cycle.

Sure:

(Catching up on emails...)

Cheers,
Miguel

author maintainer
#5 lore raw
Quoted 10 lines · Miguel Ojeda
Quoted 3 lines · Alexandre Courbot

Miguel, are you ok if we take this one through the drm-rust tree? Nova is likely to make use of it this cycle.

Sure:

(Catching up on emails...)

Actually, the nova-core code that depends on this won't cut it this cycle, so it is probably more appropriate to merge this through the Rust tree.

Sorry for the back-and-forth and last minute notice!

#6 lore raw
Quoted 6 lines · Alexandre Courbot

Actually, the nova-core code that depends on this won't cut it this cycle, so it is probably more appropriate to merge this through the Rust tree.

Sorry for the back-and-forth and last minute notice!

No worries, I can do that -- do you want that I take the second patch too?

Cheers,
Miguel

maintainer
#7 lore raw
Quoted 9 lines · Miguel Ojeda
Quoted 6 lines · Alexandre Courbot

Actually, the nova-core code that depends on this won't cut it this cycle, so it is probably more appropriate to merge this through the Rust tree.

Sorry for the back-and-forth and last minute notice!

No worries, I can do that -- do you want that I take the second patch too?

There shouldn't be a conflict and it's always nice when a feature comes directly with a user. :)

author maintainer
#8 lore raw
In reply to Miguel Ojeda
Quoted 9 lines · Miguel Ojeda
Quoted 6 lines · Alexandre Courbot

Actually, the nova-core code that depends on this won't cut it this cycle, so it is probably more appropriate to merge this through the Rust tree.

Sorry for the back-and-forth and last minute notice!

No worries, I can do that -- do you want that I take the second patch too?

If it doesn't conflict, that's probably a good idea yes!

Thanks,
Alex.

reviewer
#9 lore raw
In reply to Alexandre Courbot
Quoted 27 lines · On Mon, 15 Dec 2025 14:49:09 +0900 Alexandre Courbot <[email protected]>…

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.

---
 rust/kernel/transmute.rs | 8 ++++++++
 1 file changed, 8 insertions(+)
rust/kernel/transmute.rs +4 -0
index be5dbf3829e2..5711580c9f9b 100644
@@ -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,

rust/kernel/transmute.rs macro_rules! impl_asbytes { · line 237

Quoted 4 lines
rust/kernel/transmute.rs +1 -0
@@ -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.

Some nit: technically you can turn any ZST into bytes, even those with invariants or uninhabited. So while the comment is correct, it's kinda redundant for the object -> bytes direction :)

No change required for this patch.

Best,
Gary

Quoted 7 lines
rust/kernel/transmute.rs +3 -0
+    (),
+    {<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,
author maintainer
#10 lore raw

PATCH v4 2/2 gpu: nova-core: gsp: use () as message type for GspInitDone message

In reply to Alexandre Courbot

GspInitDone has no payload whatsoever, so the unit type () is the correct way to represent its message content. We can use it now that () implements FromBytes.

---
 drivers/gpu/nova-core/gsp/commands.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/nova-core/gsp/commands.rs +3 -3
index 0425c65b5d6f..2050771f9b53 100644
@@ -142,7 +142,7 @@ fn init_variable_payload(
 }
 
 /// Message type for GSP initialization done notification.
-struct GspInitDone {}
+struct GspInitDone;
 
 // SAFETY: `GspInitDone` is a zero-sized type with no bytes, therefore it
 // trivially has no uninitialized bytes.
@@ -151,13 +151,13 @@ unsafe impl FromBytes for GspInitDone {}
 impl MessageFromGsp for GspInitDone {
     const FUNCTION: MsgFunction = MsgFunction::GspInitDone;
     type InitError = Infallible;
-    type Message = GspInitDone;
+    type Message = ();
 
     fn read(
         _msg: &Self::Message,
         _sbuffer: &mut SBufferIter<array::IntoIter<&[u8], 2>>,
     ) -> Result<Self, Self::InitError> {
-        Ok(GspInitDone {})
+        Ok(GspInitDone)
     }
 }
 
Signature · 1 line
2.52.0
maintainer Acked-by on v4 2/2
#11 lore raw

Re: [PATCH v4 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message

Quoted 6 lines · Alexandre Courbot

GspInitDone has no payload whatsoever, so the unit type () is the correct way to represent its message content. We can use it now that () implements FromBytes.

Applied
#12 lore raw
In reply to Alexandre Courbot
Quoted 9 lines · Alexandre Courbot

This is useful in Nova's GSP message handling, as some messages are empty and we currently 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.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel