Commit 88622323 authored by Alexandre Courbot's avatar Alexandre Courbot
Browse files

rust: enable slice_flatten feature and provide it through an extension trait



In Rust 1.80, the previously unstable `slice::flatten` family of methods
have been stabilized and renamed to `slice::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 providing an
`as_flattened` implementation through an extension trait for compiler
versions where it is not available.

The trait is then exported from the prelude, making the `as_flattened`
family of methods transparently available for all supported compiler
versions.

This extension trait can be removed once the MSRV passes 1.80.

Suggested-by: default avatarMiguel Ojeda <ojeda@kernel.org>
Link: https://lore.kernel.org/all/CANiq72kK4pG=O35NwxPNoTO17oRcg1yfGcvr3==Fi4edr+sfmw@mail.gmail.com/


Acked-by: default avatarDanilo Krummrich <dakr@kernel.org>
Acked-by: default avatarMiguel Ojeda <ojeda@kernel.org>
Reviewed-by: default avatarAlice Ryhl <aliceryhl@google.com>
Signed-off-by: default avatarAlexandre Courbot <acourbot@nvidia.com>
Message-ID: <20251110-gsp_boot-v9-8-8ae4058e3c0e@nvidia.com>
Message-ID: <20251104-b4-as-flattened-v3-1-6cb9c26b45cd@nvidia.com>
parent 6b5a10da
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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

+4 −0
Original line number Diff line number Diff line
@@ -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;
+3 −0
Original line number Diff line number Diff line
@@ -51,3 +51,6 @@
pub use super::current;

pub use super::uaccess::UserPtr;

#[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))]
pub use super::slice::AsFlattened;

rust/kernel/slice.rs

0 → 100644
+49 −0
Original line number Diff line number Diff line
// 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
#[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))]
pub trait AsFlattened<T> {
    /// Takes a `&[[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(&self) -> &[T];

    /// Takes a `&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_mut(&mut self) -> &mut [T];
}

#[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))]
impl<T, const N: usize> AsFlattened<T> for [[T; N]] {
    #[allow(clippy::incompatible_msrv)]
    fn as_flattened(&self) -> &[T] {
        self.flatten()
    }

    #[allow(clippy::incompatible_msrv)]
    fn as_flattened_mut(&mut self) -> &mut [T] {
        self.flatten_mut()
    }
}