rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
Subsystems (4)
Alexandre Courbot
maintainerv3 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]>
Alexandre Courbot
maintainerPATCH 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
@@ -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
Benno Lossin
reviewerQuoted 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.
Reviewed-by Alistair Popple <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/transmute.rs | 8 ++++++++ 1 file changed, 8 insertions(+)rust/kernel/transmute.rs +0
-0
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,
Alice Ryhl
maintainerQuoted 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.
Reviewed-by Alistair Popple <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/transmute.rs | 8 ++++++++ 1 file changed, 8 insertions(+)rust/kernel/transmute.rs +0
-0@@ -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
-0impl_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
Alexandre Courbot
maintainerPATCH v3 2/2 gpu: nova-core: gsp: use () as message type for GspInitDone message
In reply to Alexandre CourbotGspInitDone 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
@@ -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
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-nextso Nova code can start using this feature quickly.Signed-off-by Alexandre Courbot <[email protected]>
Quoted 23 lines
---
Changes in v3:
- Use better safety statement. (thanks Gary!)
- Also addPhantomData. (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 messagedrivers/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-78ab58ba9e6eBest regards,