Re: [PATCH v3 02/16] rust: mem: add `transmute` with deferred size check

"Alexandre Courbot" <[email protected]>
Newsgroups dev.linux.lists.nova-gpu,dev.linux.lists.driver-core,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel,org.kernel.vger.linux-pci,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Wed Aug 19, 2026 at 8:09 PM JST, Gary Guo wrote:
> Implement a `transmute_unchecked/transmute` that checks size at
> monomorphization time instead of type-checking time. This allows more cases
> where we know that the size matches but this is not generically checkable.
>
> The signature is equivalent to the unstable `transmute_neo` function in the
> standard library. A safe variant is provided to use with types implementing
> `FromBytes` and `IntoBytes`.
>
> Existing users of `transmute_copy` to bypass size checks are converted.
>
> Signed-off-by: Gary Guo <[email protected]>

Reviewed-by: Alexandre Courbot <[email protected]>

... with a couple of minor nits below.

<...>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 4d5c96ddc49c..7225abc64084 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -98,6 +98,7 @@
>  pub mod kunit;
>  pub mod list;
>  pub mod maple_tree;
> +pub mod mem;
>  pub mod miscdevice;
>  pub mod mm;
>  pub mod module;
> diff --git a/rust/kernel/mem.rs b/rust/kernel/mem.rs
> new file mode 100644
> index 000000000000..a0901cbe1b2d
> --- /dev/null
> +++ b/rust/kernel/mem.rs
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Basic utilities for dealing with memory, values, and types.
> +
> +use crate::prelude::*;
> +
> +/// Version of `transmute` that performs size check at monomorphization-time.
> +///
> +/// Use this instead of [`core::mem::transmute`] when it is known that sizes are identical but this
> +/// cannot be proven by the compiler during type checking.
> +///
> +/// The signature is equivalent after Rust standard library's unstable `transmute_neo` and that of

nit: "equivalent to Rust..."?

> +/// [RFC 3844](https://github.com/rust-lang/rfcs/pull/3844).
> +///
> +/// # Safety
> +///
> +/// Same as [`core::mem::transmute`].

Let's add a short doctest for this new core function (and for
`transmute` as well).

> +#[inline(always)]

I suspect we want `#[inline]` here in line with Sashiko's comment on
patch 3.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.