Skip to contentSkip to conversation

rust: transmute: implement FromBytes and AsBytes for ()

PATCH Superseded Alexandre Courbot 3 participants 7 messages Dec 8, 2025 – Dec 9, 2025

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

v2 cover rust: transmute: implement FromBytes and AsBytes for ()

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.

---
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 ()
gpu: nova-core: gsp: use () as message type for GspInitDone message

 drivers/gpu/nova-core/gsp/commands.rs | 6 +++---
 rust/kernel/transmute.rs              | 6 ++++++
 2 files changed, 9 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 v2 1/2 rust: transmute: implement FromBytes and AsBytes for ()

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 | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/transmute.rs +6 -0
index be5dbf3829e2..8d78c81e3749 100644
@@ -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,
Signature · 1 line
2.52.0
Reviewed-by on v2 1/2
#2 lore raw

On 2025-12-08 at 20:54 +1100, Alexandre Courbot <[email protected]> wrote...

Quoted 24 lines

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

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

Quoted 4 lines
rust/kernel/transmute.rs +1 -0
@@ -230,6 +233,9 @@ macro_rules! impl_asbytes {
 }
 
 impl_asbytes! {
+    // SAFETY: This type is empty and thus returns an empty slice.

I would have thought the safety comment should have referenced that it cannot contain any uninitialized data. OTOH that's probably obvious so:

Quoted 9 lines
rust/kernel/transmute.rs +2 -0
+    (),
+
     // SAFETY: Instances of the following types have no uninitialized portions.
     u8, u16, u32, u64, usize,
     i8, i16, i32, i64, isize,
Signature · 2 lines
-- 
2.52.0
reviewer
#3 lore raw
In reply to Alexandre Courbot
Quoted 14 lines · On Mon, 08 Dec 2025 18:54:40 +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 | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/transmute.rs +0 -0
index be5dbf3829e2..8d78c81e3749 100644

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

Quoted 5 lines
rust/kernel/transmute.rs +2 -0
@@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
 }
 
 impl_frombytes! {
+    // SAFETY: This type is empty and thus does not consume any data.
+    (),

I'd avoid use the word "empty" as it has the meaning of uninhabited types (never type) in type theory. ZSTs that are inhabited are unit types (or singletons).

Perhaps better to have a justification that applies to all inhabited ZSTs that don't have special semantics. Something like this?

	// SAFETY: Inhabited ZSTs only have one possible bit pattern.
	(),
	{<T>} PhantomData<T>,

Best,
Gary

Quoted 15 lines
rust/kernel/transmute.rs +4 -0
+
     // 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,
author maintainer
#4 lore raw
Quoted 28 lines · Gary Guo
Quoted 14 lines · On Mon, 08 Dec 2025 18:54:40 +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 | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/transmute.rs +0 -0
index be5dbf3829e2..8d78c81e3749 100644

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

Quoted 5 lines
rust/kernel/transmute.rs +2 -0
@@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
 }
 
 impl_frombytes! {
+    // SAFETY: This type is empty and thus does not consume any data.
+    (),

I'd avoid use the word "empty" as it has the meaning of uninhabited types (never type) in type theory. ZSTs that are inhabited are unit types (or singletons).

Quoted 6 lines

Perhaps better to have a justification that applies to all inhabited ZSTs that don't have special semantics. Something like this?

	// SAFETY: Inhabited ZSTs only have one possible bit pattern.
	(),
	{<T>} PhantomData<T>,

Sounds good - I'll use your SAFETY statement as-is. Adding PhantomData is also a great idea.

Thanks for the review!

author maintainer
#5 lore raw

PATCH v2 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
Reviewed-by on v2 2/2
#6 lore raw

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

On 2025-12-08 at 20:54 +1100, Alexandre Courbot <[email protected]> wrote...

Quoted 3 lines

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.

Much nicer, thanks.

Quoted 38 lines
---
 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 · 2 lines
-- 
2.52.0