Re: [PATCH v7] rust: transmute: Add methods for FromBytes trait

From: Miguel Ojeda
Date: Sun Jun 15 2025 - 05:35:09 EST


On Sun, Jun 15, 2025 at 9:20 AM Every2 <christiansantoslima21@xxxxxxxxx> wrote:
>
> Methods receive a slice and perform size check to add a valid way to make
> conversion safe. An Option is used, in error case just return `None`.

Please start with something like "Add methods ... and ... They are
useful to ...", i.e. the idea is to try to explain the "what" and the
"why" (what is being added, and why is it being added).

> Signed-off-by: Every2 <christiansantoslima21@xxxxxxxxx>

The previous version used your name in the SoB -- is this expected?

> +/// # Example

Please use the plural: # Examples

(and empty line between it and the ```).

> +/// match result {
> +/// Some(x) => assert_eq!(*x, 0x4030201),
> +/// None => unreachable!()
> +/// }

If we are going to use something like `unreachable!()`, then I would
just unwrap.

But recently we are trying to make examples look more like normal
kernel code, so please use `?` instead.

> + && (bytes.as_ptr() as usize) % core::mem::align_of::<$t>() == 0

Can we do the cast earlier and then use `.is_aligned()` instead?

Also, since this is inside a macro, we should try to avoid assuming
anything about the caller's code, so please use `::core` instead of
`core`.

> - // SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
> - // patterns are also acceptable for arrays of that type.
> - {<T: FromBytes>} [T],
> {<T: FromBytes, const N: usize>} [T; N],

Don't we still want this safety comment for the array case?

Also, this is still missing safety comments on top of a couple
`unsafe` blocks (pointed out in an earlier version). Are you building
with `make ..... CLIPPY=1`?

Thanks!

Cheers,
Miguel