1 // SPDX-License-Identifier: GPL-2.0 2 3 //! Additional (and temporary) slice helpers. 4 5 /// Extension trait providing a portable version of [`as_flattened`] and 6 /// [`as_flattened_mut`]. 7 /// 8 /// In Rust 1.80, the previously unstable `slice::flatten` family of methods 9 /// have been stabilized and renamed from `flatten` to `as_flattened`. 10 /// 11 /// This creates an issue for as long as the MSRV is < 1.80, as the same functionality is provided 12 /// by different methods depending on the compiler version. 13 /// 14 /// This extension trait solves this by abstracting `as_flatten` and calling the correct method 15 /// depending on the Rust version. 16 /// 17 /// This trait can be removed once the MSRV passes 1.80. 18 /// 19 /// [`as_flattened`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened 20 /// [`as_flattened_mut`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened_mut 21 #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))] 22 pub trait AsFlattened<T> { 23 /// Takes a `&[[T; N]]` and flattens it to a `&[T]`. 24 /// 25 /// This is an portable layer on top of [`as_flattened`]; see its documentation for details. 26 /// 27 /// [`as_flattened`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened 28 fn as_flattened(&self) -> &[T]; 29 30 /// Takes a `&mut [[T; N]]` and flattens it to a `&mut [T]`. 31 /// 32 /// This is an portable layer on top of [`as_flattened_mut`]; see its documentation for details. 33 /// 34 /// [`as_flattened_mut`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened_mut 35 fn as_flattened_mut(&mut self) -> &mut [T]; 36 } 37 38 #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))] 39 impl<T, const N: usize> AsFlattened<T> for [[T; N]] { 40 #[allow(clippy::incompatible_msrv)] 41 fn as_flattened(&self) -> &[T] { 42 self.flatten() 43 } 44 45 #[allow(clippy::incompatible_msrv)] 46 fn as_flattened_mut(&mut self) -> &mut [T] { 47 self.flatten_mut() 48 } 49 } 50