summaryrefslogtreecommitdiff
path: root/rust/kernel/slice.rs
diff options
context:
space:
mode:
authorDave Airlie <airlied@redhat.com>2025-11-20 10:42:50 +1000
committerDave Airlie <airlied@redhat.com>2025-11-20 10:44:50 +1000
commitf0ded972d37150f9f889de75c9eecc5cb0730013 (patch)
tree1cbf2650fd192cd2b10d497ada78dc55721d842a /rust/kernel/slice.rs
parentf3a1d69f9b388271986f4efe1fd775df15b443c1 (diff)
parent77b686f688126a5f758b51441a03186e9eb1b0f1 (diff)
Merge tag 'drm-rust-next-2025-11-18' of https://gitlab.freedesktop.org/drm/rust/kernel into drm-next
Cross-subsystem Changes: Rust - Make slice::as_flattened usable on all supported versions of rustc. - Add FromBytes::from_bytes_prefix() method. Core Changes: - Update Tyr in MAINTAINERS file. - Remove redundant device ptr from Rust GEM object. - Change how AlwaysRefCounted is implemented for GEM objects. - Add deferred vm_bo cleanup to GPUVM and use it in Panthor. Driver Changes: Nova Core - Introduction of bitfield! macro, with support for different storage sizes and custom visibility. - Introduction of safe converters between integer types for which the conversion is lossless. - GSP initialized up to fully booted state on Ampere. - Use more future-proof register for GPU identification. - Various simplifications and optimizations. Nova - Select NOVA_CORE. - Depend on CONFIG_64BIT. Signed-off-by: Dave Airlie <airlied@redhat.com> From: Alice Ryhl <aliceryhl@google.com> Link: https://patch.msgid.link/aRxtJC0D1pQUepF4@google.com
Diffstat (limited to 'rust/kernel/slice.rs')
-rw-r--r--rust/kernel/slice.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/rust/kernel/slice.rs b/rust/kernel/slice.rs
new file mode 100644
index 000000000000..6ca91a4fd1f2
--- /dev/null
+++ b/rust/kernel/slice.rs
@@ -0,0 +1,49 @@
+// 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()
+ }
+}