[PATCH v3 1/2] rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs

Alexandre Courbot posted 2 patches 1 week, 3 days ago
There is a newer version of this series
[PATCH v3 1/2] rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
Posted by Alexandre Courbot 1 week, 3 days ago
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 <apopple@nvidia.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/transmute.rs | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index be5dbf3829e2..a888a312e7ff 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -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,

-- 
2.52.0
Re: [PATCH v3 1/2] rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
Posted by Benno Lossin 1 week, 2 days ago
On Tue Dec 9, 2025 at 3:57 AM CET, Alexandre Courbot wrote:
> 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 <apopple@nvidia.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/transmute.rs | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index be5dbf3829e2..a888a312e7ff 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -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

> +    (),
> +    {<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,
Re: [PATCH v3 1/2] rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
Posted by Alice Ryhl 1 week, 1 day ago
On Tue, Dec 09, 2025 at 01:14:08PM +0100, Benno Lossin wrote:
> On Tue Dec 9, 2025 at 3:57 AM CET, Alexandre Courbot wrote:
> > 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 <apopple@nvidia.com>
> > Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> > ---
> >  rust/kernel/transmute.rs | 8 ++++++++
> >  1 file changed, 8 insertions(+)
> >
> > diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> > index be5dbf3829e2..a888a312e7ff 100644
> > --- a/rust/kernel/transmute.rs
> > +++ b/rust/kernel/transmute.rs
> > @@ -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.

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

Alice