Re: [PATCH v10] rust: transmute: Add methods for FromBytes trait
From: Alice Ryhl
Date: Mon Aug 25 2025 - 06:25:37 EST
On Sun, Aug 24, 2025 at 06:31:33PM -0300, Christian S. Lima wrote:
> The two methods added take a slice of bytes and return those bytes in
> a specific type. These methods are useful when we need to transform
> the stream of bytes into specific type.
>
> Since the `is_aligned` method for pointer types has been stabilized in
> `1.79` version and is being used in this patch. I'm enabling the
> feature. In this case, using this method is useful to check the
> alignment and avoid a giant boilerplate, such as `(foo.as_ptr() as
> usize) % core::mem::align_of::<T>() == 0`.
>
> Even enabling in `rust/kernel/lib.rs` when compiling with `make LLVM=1
> CLIPPY=1` a warning is issued, so in order to compile, it was used
> locally the `#[allow(clippy::incompatible_msrv)]`.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1119
> Suggested-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
> Signed-off-by: Christian S. Lima <christiansantoslima21@xxxxxxxxx>
With my comments addressed:
Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> -pub unsafe trait FromBytes {}
> +pub unsafe trait FromBytes {
> + /// Converts a slice of bytes to a reference to `Self`.
> + ///
> + /// When the reference is properly aligned and the size of slice is equal to that of `T`
> + /// and is different from zero.
> + ///
> + /// In another case, it will return `None`.
> + #[allow(clippy::incompatible_msrv)]
Does the warning appear on all configurations? If so, this #[allow]
should be an #[expect].
> macro_rules! impl_frombytes {
> ($($({$($generics:tt)*})? $t:ty, )*) => {
> @@ -28,7 +94,6 @@ macro_rules! impl_frombytes {
>
> // 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],
Why is this impl removed? I would think that with the Self: Sized
bounds, you don't need to remove it anymore.
Alice