Skip to contentSkip to conversation

rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs

PATCH Superseded Alexandre Courbot 4 participants 6 messages Dec 9, 2025 – Dec 11, 2025

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

v3 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 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: ba65a4e7120a616d9c592750d9147f6dcafedffa
change-id: 20251208-transmute_unit-78ab58ba9e6e

Best regards,

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

PATCH v3 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..a888a312e7ff 100644
@@ -170,6 +170,10 @@ macro_rules! impl_frombytes {
 }
 
 impl_frombytes! {
+    // SAFETY: Inhabited ZSTs only have one possible bit pattern.
+    (),
+    {<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.
+    (),
+    {<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
#2 lore raw
Quoted 15 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.

---
 rust/kernel/transmute.rs | 8 ++++++++
 1 file changed, 8 insertions(+)
rust/kernel/transmute.rs +0 -0
index be5dbf3829e2..a888a312e7ff 100644

rust/kernel/transmute.rs macro_rules! impl_frombytes { · line 173

Quoted 4 lines
rust/kernel/transmute.rs +1 -0
@@ -170,6 +170,10 @@ macro_rules! impl_frombytes {
 }
 
 impl_frombytes! {
+    // SAFETY: Inhabited ZSTs only have one possible bit pattern.

Even inhabited ZSTs cannot just be conjured out of thin air, since there might be safety invariants on the creation of such a type. Now these two concrete ones do not have any, so this is fine, but should still be mentioned in the comment IMO.

Cheers,
Benno

Quoted 6 lines
rust/kernel/transmute.rs +3 -0
+    (),
+    {<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,
maintainer
#3 lore raw
Quoted 18 lines · Benno Lossin
Quoted 17 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.

---
 rust/kernel/transmute.rs | 8 ++++++++
 1 file changed, 8 insertions(+)
rust/kernel/transmute.rs +0 -0
index be5dbf3829e2..a888a312e7ff 100644
@@ -170,6 +170,10 @@ macro_rules! impl_frombytes {
 }
 
Quoted 7 lines

rust/kernel/transmute.rs macro_rules! impl_frombytes { · line 173

Quoted 2 lines
rust/kernel/transmute.rs +1 -0
 impl_frombytes! {
+    // SAFETY: Inhabited ZSTs only have one possible bit pattern.

Even inhabited ZSTs cannot just be conjured out of thin air, since there might be safety invariants on the creation of such a type. Now these two concrete ones do not have any, so this is fine, but should still be mentioned in the comment IMO.

I think we can say "it has no invariants".

Alice

author maintainer
#4 lore raw

PATCH v3 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
reviewer Reviewed-by on the v3 series
#5 lore raw
In reply to Alexandre Courbot
Quoted 8 lines · On Tue, 09 Dec 2025 11:57:58 +0900 Alexandre Courbot <[email protected]>…

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.

Quoted 23 lines

---
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: ba65a4e7120a616d9c592750d9147f6dcafedffa
change-id: 20251208-transmute_unit-78ab58ba9e6e

Best regards,