From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Alice Ryhl" <aliceryhl@google.com>,
"Alexandre Courbot" <acourbot@nvidia.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nicolas Schier" <nicolas.schier@linux.dev>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-kbuild@vger.kernel.org
Subject: Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
Date: Tue, 04 Nov 2025 00:06:54 +0900 [thread overview]
Message-ID: <DDZ5J5OC26UH.1B8CGNEJGRTS5@nvidia.com> (raw)
In-Reply-To: <aQiCeZE7eN_KVdZ5@google.com>
On Mon Nov 3, 2025 at 7:22 PM JST, Alice Ryhl wrote:
> On Sat, Nov 01, 2025 at 10:31:38PM +0900, Alexandre Courbot wrote:
>> In Rust 1.80, the previously unstable `slice::flatten` family of methods
>> have been stabilized and renamed to `as_flattened`.
>>
>> This creates an issue as we want to use `as_flattened`, but need to
>> support the MSRV (which at the moment is Rust 1.78) where it is named
>> `flatten`.
>>
>> Solve this by enabling the `slice_flatten` feature, and abstracting
>> `as_flatten` behind an extension trait that calls the right method
>> depending on the Rust version.
>>
>> This extension trait can be removed once the MSRV passes 1.80.
>>
>> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
>> Link: https://lore.kernel.org/all/CANiq72kK4pG=O35NwxPNoTO17oRcg1yfGcvr3==Fi4edr+sfmw@mail.gmail.com/
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>> This patch was part of the Nova GSP boot series [1], but since it
>> requires attention from the core Rust team (and possibly the build
>> maintainers?) and is otherwise buried under Nova patches, I am taking
>> the freedom to send it separately for visibility.
>>
>> Hopefully it captures Miguel's suggestion [2] accurately, but please let
>> me know if I missed something.
>>
>> Since the Nova GSP boot series makes use of this, I hope to eventually
>> merge it alongside the series, through the DRM tree.
>>
>> Thanks!
>>
>> [1] https://lore.kernel.org/all/20251029-gsp_boot-v7-0-34227afad347@nvidia.com/
>> [2] https://lore.kernel.org/all/CANiq72kK4pG=O35NwxPNoTO17oRcg1yfGcvr3==Fi4edr+sfmw@mail.gmail.com/
>> ---
>> init/Kconfig | 3 +++
>> rust/kernel/lib.rs | 4 ++++
>> rust/kernel/slice.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
>> scripts/Makefile.build | 3 ++-
>> 4 files changed, 72 insertions(+), 1 deletion(-)
>>
>> diff --git a/init/Kconfig b/init/Kconfig
>> index cab3ad28ca49..7da93c9cccc3 100644
>> --- a/init/Kconfig
>> +++ b/init/Kconfig
>> @@ -147,6 +147,9 @@ config LD_CAN_USE_KEEP_IN_OVERLAY
>> # https://github.com/llvm/llvm-project/pull/130661
>> def_bool LD_IS_BFD || LLD_VERSION >= 210000
>>
>> +config RUSTC_HAS_SLICE_AS_FLATTENED
>> + def_bool RUSTC_VERSION >= 108000
>> +
>> config RUSTC_HAS_COERCE_POINTEE
>> def_bool RUSTC_VERSION >= 108400
>>
>> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
>> index 3dd7bebe7888..2581a356d114 100644
>> --- a/rust/kernel/lib.rs
>> +++ b/rust/kernel/lib.rs
>> @@ -21,6 +21,9 @@
>> #![feature(inline_const)]
>> #![feature(pointer_is_aligned)]
>> //
>> +// Stable since Rust 1.80.0.
>> +#![feature(slice_flatten)]
>> +//
>> // Stable since Rust 1.81.0.
>> #![feature(lint_reasons)]
>> //
>> @@ -128,6 +131,7 @@
>> pub mod security;
>> pub mod seq_file;
>> pub mod sizes;
>> +pub mod slice;
>> mod static_assert;
>> #[doc(hidden)]
>> pub mod std_vendor;
>> diff --git a/rust/kernel/slice.rs b/rust/kernel/slice.rs
>> new file mode 100644
>> index 000000000000..7e837bec4bed
>> --- /dev/null
>> +++ b/rust/kernel/slice.rs
>> @@ -0,0 +1,63 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +//! Additional (and temporary) slice helpers.
>> +
>> +/// Extension trait providing a portable version of [`as_flattened`] and
>> +/// [`as_flattened_mut`].
>> +///
>> +/// In Rust 1.80, the previously unstable `slice::flatten` family of methods
>> +/// have been stabilized and renamed from `flatten` to `as_flattened`.
>> +///
>> +/// This creates an issue for as long as the MSRV is < 1.80, as the same functionality is provided
>> +/// by different methods depending on the compiler version.
>> +///
>> +/// This extension trait solves this by abstracting `as_flatten` and calling the correct method
>> +/// depending on the Rust version.
>> +///
>> +/// This trait can be removed once the MSRV passes 1.80.
>> +///
>> +/// [`as_flattened`]: slice::as_flattened
>> +/// [`as_flattened_mut`]: slice::as_flattened_mut
>> +pub trait AsFlattened<T> {
>> + /// Takes an `&[[T; N]]` and flattens it to a `&[T]`.
>> + ///
>> + /// This is an portable layer on top of [`as_flattened`]; see its documentation for details.
>> + ///
>> + /// [`as_flattened`]: slice::as_flattened
>> + fn as_flattened_slice(&self) -> &[T];
>> +
>> + /// Takes an `&mut [[T; N]]` and flattens it to a `&mut [T]`.
>> + ///
>> + /// This is an portable layer on top of [`as_flattened_mut`]; see its documentation for details.
>> + ///
>> + /// [`as_flattened_mut`]: slice::as_flattened_mut
>> + fn as_flattened_slice_mut(&mut self) -> &mut [T];
>> +}
>> +
>> +impl<T, const N: usize> AsFlattened<T> for [[T; N]] {
>> + #[allow(clippy::incompatible_msrv)]
>> + fn as_flattened_slice(&self) -> &[T] {
>> + #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))]
>> + {
>> + self.flatten()
>> + }
>> +
>> + #[cfg(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED)]
>> + {
>> + self.as_flattened()
>> + }
>> + }
>> +
>> + #[allow(clippy::incompatible_msrv)]
>> + fn as_flattened_slice_mut(&mut self) -> &mut [T] {
>> + #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))]
>> + {
>> + self.flatten_mut()
>> + }
>> +
>> + #[cfg(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED)]
>> + {
>> + self.as_flattened_mut()
>> + }
>
> Hmm. Why not have this match the name that this was stabilized under?
> That way, when bumping the MSRV, we can just remove this trait without
> changing the callers.
I expected that doing so would clash with the methods of the same name
in `slice` on Rust >= 1.80, but instead I just got a warning that my
`AsFlattened` import was ignored - it looks like Rust did the right
thing and picked the non-trait method by default.
So all we need to do to make your proposal work is to make the import
conditional on Rust being < 1.80. And we can also make the whole trait
and its impl block that way.
That would actually be much cleaner! Thanks a lot.
next prev parent reply other threads:[~2025-11-03 15:07 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-01 13:31 [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait Alexandre Courbot
2025-11-01 15:36 ` Miguel Ojeda
2025-11-02 2:25 ` Alexandre Courbot
2025-11-02 12:00 ` Miguel Ojeda
2025-11-02 14:28 ` Alexandre Courbot
2025-11-01 15:52 ` Danilo Krummrich
2025-11-03 10:22 ` Alice Ryhl
2025-11-03 15:06 ` Alexandre Courbot [this message]
2025-11-04 8:37 ` Alice Ryhl
2025-11-04 10:57 ` Alexandre Courbot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DDZ5J5OC26UH.1B8CGNEJGRTS5@nvidia.com \
--to=acourbot@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nathan@kernel.org \
--cc=nicolas.schier@linux.dev \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox