[PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()

Alexandre Courbot posted 2 patches 1 week, 4 days ago
[PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
Posted by Alexandre Courbot 1 week, 4 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.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/transmute.rs | 6 ++++++
 1 file changed, 6 insertions(+)

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

-- 
2.52.0
Re: [PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
Posted by Gary Guo 1 week, 3 days ago
On Mon, 08 Dec 2025 18:54:40 +0900
Alexandre Courbot <acourbot@nvidia.com> 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.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/transmute.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index be5dbf3829e2..8d78c81e3749 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -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
	
> +
>      // 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,
>
Re: [PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
Posted by Alexandre Courbot 1 week, 3 days ago
On Mon Dec 8, 2025 at 10:27 PM JST, Gary Guo wrote:
> On Mon, 08 Dec 2025 18:54:40 +0900
> Alexandre Courbot <acourbot@nvidia.com> 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.
>> 
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>>  rust/kernel/transmute.rs | 6 ++++++
>>  1 file changed, 6 insertions(+)
>> 
>> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
>> index be5dbf3829e2..8d78c81e3749 100644
>> --- a/rust/kernel/transmute.rs
>> +++ b/rust/kernel/transmute.rs
>> @@ -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>,

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

Thanks for the review!
Re: [PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
Posted by Alistair Popple 1 week, 4 days ago
On 2025-12-08 at 20:54 +1100, Alexandre Courbot <acourbot@nvidia.com> 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.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/transmute.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index be5dbf3829e2..8d78c81e3749 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -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.

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

Reviewed-by: Alistair Popple <apopple@nvidia.com>

> +    (),
> +
>      // SAFETY: Instances of the following types have no uninitialized portions.
>      u8, u16, u32, u64, usize,
>      i8, i16, i32, i64, isize,
> 
> -- 
> 2.52.0
>