rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
Subsystems (4)
Alexandre Courbot
maintainerv4 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]>
Alexandre Courbot
maintainerPATCH 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
@@ -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
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.
Reviewed-by Alistair Popple <[email protected]>Reviewed-by Gary Guo <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>
Cheers,
Benno
Quoted 3 lines
--- rust/kernel/transmute.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
Alexandre Courbot
maintainerQuoted 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.
Reviewed-by Alistair Popple <[email protected]>Reviewed-by Gary Guo <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>Reviewed-by Benno Lossin <[email protected]>
Miguel, are you ok if we take this one through the drm-rust tree? Nova is likely to make use of it this cycle.
Miguel Ojeda
Acked-byQuoted 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
Alexandre Courbot
maintainerQuoted 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:
Acked-by Miguel Ojeda <[email protected]>(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!
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
Danilo Krummrich
maintainerQuoted 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. :)
Alexandre Courbot
maintainerQuoted 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.
Gary Guo
reviewerQuoted 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.
Reviewed-by Alistair Popple <[email protected]>Reviewed-by Gary Guo <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/transmute.rs | 8 ++++++++ 1 file changed, 8 insertions(+)rust/kernel/transmute.rs +4
-0@@ -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,
Alexandre Courbot
maintainerPATCH v4 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
Re: [PATCH v4 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message
Quoted 6 lines · Alexandre Courbot
GspInitDonehas no payload whatsoever, so the unit type()is the correct way to represent its message content. We can use it now that()implementsFromBytes.Reviewed-by Alistair Popple <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>
Miguel Ojeda
AppliedQuoted 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-nextso Nova code can start using this feature quickly.Signed-off-by Alexandre Courbot <[email protected]>
Applied to rust-next -- thanks everyone!
Cheers,
Miguel