* [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
@ 2025-11-01 13:31 Alexandre Courbot
2025-11-01 15:36 ` Miguel Ojeda
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Alexandre Courbot @ 2025-11-01 13:31 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Nathan Chancellor, Nicolas Schier
Cc: linux-kernel, rust-for-linux, linux-kbuild, Alexandre Courbot
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()
+ }
+ }
+}
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index d0ee33a487be..a84b9e3171a3 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -308,6 +308,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE
# The features in this list are the ones allowed for non-`rust/` code.
#
+# - Stable since Rust 1.80.0: `feature(slice_flatten)`.
# - Stable since Rust 1.81.0: `feature(lint_reasons)`.
# - Stable since Rust 1.82.0: `feature(asm_const)`,
# `feature(offset_of_nested)`, `feature(raw_ref_op)`.
@@ -317,7 +318,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE
#
# Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on
# the unstable features in use.
-rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,offset_of_nested,raw_ref_op,used_with_arg
+rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,offset_of_nested,raw_ref_op,slice_flatten,used_with_arg
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
# current working directory, which may be not accessible in the out-of-tree
---
base-commit: 9a3c2f8a4f84960a48c056d0da88de3d09e6d622
change-id: 20251101-b4-as-flattened-0b3e0f566836
Best regards,
--
Alexandre Courbot <acourbot@nvidia.com>
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
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-01 15:52 ` Danilo Krummrich
2025-11-03 10:22 ` Alice Ryhl
2 siblings, 1 reply; 10+ messages in thread
From: Miguel Ojeda @ 2025-11-01 15:36 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Nathan Chancellor, Nicolas Schier, linux-kernel,
rust-for-linux, linux-kbuild
On Sat, Nov 1, 2025 at 2:32 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> Hopefully it captures Miguel's suggestion [2] accurately, but please let
> me know if I missed something.
Yeah, this is what I meant and looks great -- thanks!
If you need to use it this cycle in another branch:
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Otherwise, I will pick it up.
> +config RUSTC_HAS_SLICE_AS_FLATTENED
I guess you used this one since they renamed it and since we don't use
the `alloc` method. It is fine, both options are confusing in
different ways, but sometimes the feature name is the only one that
can be used (since it may enable several methods etc.), so I wonder if
we should try to use that consistently.
> +/// 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.
These paragraphs sound like implementations details -- I would
probably leave that to the commit message or normal comments instead
(we should notice we need to remove these thanks to the line in
`Kconfig` already).
Nit: two spaces above.
> + /// Takes an `&[[T; N]]` and flattens it to a `&[T]`.
Nit: I don't know how one is supposed to pronounce these, but I guess
it is "a" in the first one, like the second one (the upstream docs
also do that).
By the way, it crossed my mind that we may want to use `#[doc(alias =
"...")` here to guide search, but I guess not many developers are
using local/older versions and this will go away soon anyway.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
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-01 15:52 ` Danilo Krummrich
2025-11-03 10:22 ` Alice Ryhl
2 siblings, 0 replies; 10+ messages in thread
From: Danilo Krummrich @ 2025-11-01 15:52 UTC (permalink / raw)
To: Alexandre Courbot, Miguel Ojeda
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Nathan Chancellor,
Nicolas Schier, linux-kernel, rust-for-linux, linux-kbuild
On 11/1/25 2:31 PM, Alexandre Courbot wrote:
> Since the Nova GSP boot series makes use of this, I hope to eventually
> merge it alongside the series, through the DRM tree.
If Miguel agrees, it's fine for me to take it through the drm-rust tree.
Acked-by: Danilo Krummrich <dakr@kernel.org>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
2025-11-01 15:36 ` Miguel Ojeda
@ 2025-11-02 2:25 ` Alexandre Courbot
2025-11-02 12:00 ` Miguel Ojeda
0 siblings, 1 reply; 10+ messages in thread
From: Alexandre Courbot @ 2025-11-02 2:25 UTC (permalink / raw)
To: Miguel Ojeda, Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Nathan Chancellor, Nicolas Schier, linux-kernel,
rust-for-linux, linux-kbuild
On Sun Nov 2, 2025 at 12:36 AM JST, Miguel Ojeda wrote:
> On Sat, Nov 1, 2025 at 2:32 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>
>> Hopefully it captures Miguel's suggestion [2] accurately, but please let
>> me know if I missed something.
>
> Yeah, this is what I meant and looks great -- thanks!
>
> If you need to use it this cycle in another branch:
>
> Acked-by: Miguel Ojeda <ojeda@kernel.org>
>
> Otherwise, I will pick it up.
Thanks!
>
>> +config RUSTC_HAS_SLICE_AS_FLATTENED
>
> I guess you used this one since they renamed it and since we don't use
> the `alloc` method. It is fine, both options are confusing in
> different ways, but sometimes the feature name is the only one that
> can be used (since it may enable several methods etc.), so I wonder if
> we should try to use that consistently.
Ah, I think I see what you mean. The `slice_flatten` feature has been
removed in 1.80 with the stabilization and method rename, so I guess we
would have to do something like:
config RUSTC_HAS_SLICE_FLATTEN
def_bool RUSTC_VERSION < 108000
... and invert the logic in the code. That would run contrary to the
other `RUSTC_HAS` options requiring at least a given version though.
Also not all of these seem to be tied to a feature;
`RUSTC_HAS_FILE_AS_C_STR` appears to refer to the
`Location::file_as_c_str` method, which is behind the `file_with_nul`
feature.
So I guess `config RUST_HAS_AS_FLATTENED` would also work. I'm happy to
follow what you think is best here. :)
>
>> +/// 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.
>
> These paragraphs sound like implementations details -- I would
> probably leave that to the commit message or normal comments instead
> (we should notice we need to remove these thanks to the line in
> `Kconfig` already).
>
> Nit: two spaces above.
>
>> + /// Takes an `&[[T; N]]` and flattens it to a `&[T]`.
>
> Nit: I don't know how one is supposed to pronounce these, but I guess
> it is "a" in the first one, like the second one (the upstream docs
> also do that).
The funny thing is that I intented to copy the Rust doc for
`as_flattened`, but failed to even copy/paste it properly! ^_^; Fixed,
thanks.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
2025-11-02 2:25 ` Alexandre Courbot
@ 2025-11-02 12:00 ` Miguel Ojeda
2025-11-02 14:28 ` Alexandre Courbot
0 siblings, 1 reply; 10+ messages in thread
From: Miguel Ojeda @ 2025-11-02 12:00 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Nathan Chancellor, Nicolas Schier, linux-kernel,
rust-for-linux, linux-kbuild
On Sun, Nov 2, 2025 at 3:25 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> Also not all of these seem to be tied to a feature;
> `RUSTC_HAS_FILE_AS_C_STR` appears to refer to the
> `Location::file_as_c_str` method, which is behind the `file_with_nul`
> feature.
So what happens there is that there are 3 states: "not implemented",
"unstable" and "unstable but changed". That is, one transition is when
the stdlib added it and the other when it changed.
Now, normally there are only 2: "not implemented" and "unstable", and
thus using the feature name makes sense there, because we use the
Kconfig symbol to enable the feature conditionally, not individual
methods (and a feature may contain different methods or even language
features etc.).
There is also the "stable" state of course -- the features don't get
removed internally when stabilized, and in fact there is a lint that
tells you about it (using that information), but we allow it, so it
all works without extra complexity.
In this case, we have 2 states but the other ones: "unstable" and
"unstable but changed", because `slice_flatten` exists for way longer
than our MSRV. And for this transition, for the other feature, we used
the renamed method name, which makes sense since that is what got
renamed, e.g. it could happen that a feature has 10 methods, and 2 get
renamed in version X.
So I think what you have in your patch actually makes more sense,
since you are in the rename transition, not the feature addition one
(i.e. we unconditionally enable `slice_flatten`).
I guess we could come up with a different naming scheme to distinguish
both cases or similar. For instance, we could add `FEATURE_` to the
cases of the first transition. Anyway, it is not a big deal since we
don't expect to have a ton of these -- quite the opposite, hopefully
we can get rid them soon. And I wouldn't invert the logic of those
equations, I think that gets even more confusing.
So apologies for the noise there :) At least I hope it clarifies a bit...
Cheers,
Miguel
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
2025-11-02 12:00 ` Miguel Ojeda
@ 2025-11-02 14:28 ` Alexandre Courbot
0 siblings, 0 replies; 10+ messages in thread
From: Alexandre Courbot @ 2025-11-02 14:28 UTC (permalink / raw)
To: Miguel Ojeda, Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Nathan Chancellor, Nicolas Schier, linux-kernel,
rust-for-linux, linux-kbuild
On Sun Nov 2, 2025 at 9:00 PM JST, Miguel Ojeda wrote:
> On Sun, Nov 2, 2025 at 3:25 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>
>> Also not all of these seem to be tied to a feature;
>> `RUSTC_HAS_FILE_AS_C_STR` appears to refer to the
>> `Location::file_as_c_str` method, which is behind the `file_with_nul`
>> feature.
>
> So what happens there is that there are 3 states: "not implemented",
> "unstable" and "unstable but changed". That is, one transition is when
> the stdlib added it and the other when it changed.
>
> Now, normally there are only 2: "not implemented" and "unstable", and
> thus using the feature name makes sense there, because we use the
> Kconfig symbol to enable the feature conditionally, not individual
> methods (and a feature may contain different methods or even language
> features etc.).
>
> There is also the "stable" state of course -- the features don't get
> removed internally when stabilized, and in fact there is a lint that
> tells you about it (using that information), but we allow it, so it
> all works without extra complexity.
>
> In this case, we have 2 states but the other ones: "unstable" and
> "unstable but changed", because `slice_flatten` exists for way longer
> than our MSRV. And for this transition, for the other feature, we used
> the renamed method name, which makes sense since that is what got
> renamed, e.g. it could happen that a feature has 10 methods, and 2 get
> renamed in version X.
>
> So I think what you have in your patch actually makes more sense,
> since you are in the rename transition, not the feature addition one
> (i.e. we unconditionally enable `slice_flatten`).
>
> I guess we could come up with a different naming scheme to distinguish
> both cases or similar. For instance, we could add `FEATURE_` to the
> cases of the first transition. Anyway, it is not a big deal since we
> don't expect to have a ton of these -- quite the opposite, hopefully
> we can get rid them soon. And I wouldn't invert the logic of those
> equations, I think that gets even more confusing.
>
> So apologies for the noise there :) At least I hope it clarifies a bit...
It does - thanks for taking the time to explain!
I'll leave the config option name as it currently then.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
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-01 15:52 ` Danilo Krummrich
@ 2025-11-03 10:22 ` Alice Ryhl
2025-11-03 15:06 ` Alexandre Courbot
2 siblings, 1 reply; 10+ messages in thread
From: Alice Ryhl @ 2025-11-03 10:22 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
Nathan Chancellor, Nicolas Schier, linux-kernel, rust-for-linux,
linux-kbuild
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.
Alice
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
2025-11-03 10:22 ` Alice Ryhl
@ 2025-11-03 15:06 ` Alexandre Courbot
2025-11-04 8:37 ` Alice Ryhl
0 siblings, 1 reply; 10+ messages in thread
From: Alexandre Courbot @ 2025-11-03 15:06 UTC (permalink / raw)
To: Alice Ryhl, Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
Nathan Chancellor, Nicolas Schier, linux-kernel, rust-for-linux,
linux-kbuild
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.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
2025-11-03 15:06 ` Alexandre Courbot
@ 2025-11-04 8:37 ` Alice Ryhl
2025-11-04 10:57 ` Alexandre Courbot
0 siblings, 1 reply; 10+ messages in thread
From: Alice Ryhl @ 2025-11-04 8:37 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
Nathan Chancellor, Nicolas Schier, linux-kernel, rust-for-linux,
linux-kbuild
On Tue, Nov 04, 2025 at 12:06:54AM +0900, Alexandre Courbot wrote:
> 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.
One option is to put the trait in the prelude on relevant rustc
versions. That way, users of the method don't need any conditional
logic.
Alice
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait
2025-11-04 8:37 ` Alice Ryhl
@ 2025-11-04 10:57 ` Alexandre Courbot
0 siblings, 0 replies; 10+ messages in thread
From: Alexandre Courbot @ 2025-11-04 10:57 UTC (permalink / raw)
To: Alice Ryhl, Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
Nathan Chancellor, Nicolas Schier, linux-kernel, rust-for-linux,
linux-kbuild
On Tue Nov 4, 2025 at 5:37 PM JST, Alice Ryhl wrote:
> On Tue, Nov 04, 2025 at 12:06:54AM +0900, Alexandre Courbot wrote:
>> 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.
>
> One option is to put the trait in the prelude on relevant rustc
> versions. That way, users of the method don't need any conditional
> logic.
And with that last touch I think we have reached perfection! :) Thanks
again.
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2025-11-04 10:57 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2025-11-04 8:37 ` Alice Ryhl
2025-11-04 10:57 ` Alexandre Courbot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).