Re: [PATCH v3 02/16] rust: mem: add `transmute` with deferred size check
From: Gary Guo
Date: Thu Aug 27 2026 - 10:26:34 EST
On Thu Aug 27, 2026 at 7:24 AM BST, Alexandre Courbot wrote:
> 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 <gary@xxxxxxxxxxx>
>
> Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
>
> ... 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.
That's a known false positive of Sashiko (latest prompt should have fixed it).
For this function I really want it to *never* be not inlined (it's supposed to
compile down to, well, nothing), so for example, it's allowed to rely on this
function for code that use build_assert.
Best,
Gary