188622323SAlexandre Courbot // SPDX-License-Identifier: GPL-2.0 288622323SAlexandre Courbot 388622323SAlexandre Courbot //! Additional (and temporary) slice helpers. 488622323SAlexandre Courbot 588622323SAlexandre Courbot /// Extension trait providing a portable version of [`as_flattened`] and 688622323SAlexandre Courbot /// [`as_flattened_mut`]. 788622323SAlexandre Courbot /// 888622323SAlexandre Courbot /// In Rust 1.80, the previously unstable `slice::flatten` family of methods 988622323SAlexandre Courbot /// have been stabilized and renamed from `flatten` to `as_flattened`. 1088622323SAlexandre Courbot /// 1188622323SAlexandre Courbot /// This creates an issue for as long as the MSRV is < 1.80, as the same functionality is provided 1288622323SAlexandre Courbot /// by different methods depending on the compiler version. 1388622323SAlexandre Courbot /// 1488622323SAlexandre Courbot /// This extension trait solves this by abstracting `as_flatten` and calling the correct method 1588622323SAlexandre Courbot /// depending on the Rust version. 1688622323SAlexandre Courbot /// 1788622323SAlexandre Courbot /// This trait can be removed once the MSRV passes 1.80. 1888622323SAlexandre Courbot /// 19*57dc2ea0SMiguel Ojeda /// [`as_flattened`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened 20*57dc2ea0SMiguel Ojeda /// [`as_flattened_mut`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened_mut 2188622323SAlexandre Courbot #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))] 2288622323SAlexandre Courbot pub trait AsFlattened<T> { 2388622323SAlexandre Courbot /// Takes a `&[[T; N]]` and flattens it to a `&[T]`. 2488622323SAlexandre Courbot /// 2588622323SAlexandre Courbot /// This is an portable layer on top of [`as_flattened`]; see its documentation for details. 2688622323SAlexandre Courbot /// 27*57dc2ea0SMiguel Ojeda /// [`as_flattened`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened as_flattened(&self) -> &[T]2888622323SAlexandre Courbot fn as_flattened(&self) -> &[T]; 2988622323SAlexandre Courbot 3088622323SAlexandre Courbot /// Takes a `&mut [[T; N]]` and flattens it to a `&mut [T]`. 3188622323SAlexandre Courbot /// 3288622323SAlexandre Courbot /// This is an portable layer on top of [`as_flattened_mut`]; see its documentation for details. 3388622323SAlexandre Courbot /// 34*57dc2ea0SMiguel Ojeda /// [`as_flattened_mut`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened_mut as_flattened_mut(&mut self) -> &mut [T]3588622323SAlexandre Courbot fn as_flattened_mut(&mut self) -> &mut [T]; 3688622323SAlexandre Courbot } 3788622323SAlexandre Courbot 3888622323SAlexandre Courbot #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))] 3988622323SAlexandre Courbot impl<T, const N: usize> AsFlattened<T> for [[T; N]] { 4088622323SAlexandre Courbot #[allow(clippy::incompatible_msrv)] as_flattened(&self) -> &[T]4188622323SAlexandre Courbot fn as_flattened(&self) -> &[T] { 4288622323SAlexandre Courbot self.flatten() 4388622323SAlexandre Courbot } 4488622323SAlexandre Courbot 4588622323SAlexandre Courbot #[allow(clippy::incompatible_msrv)] as_flattened_mut(&mut self) -> &mut [T]4688622323SAlexandre Courbot fn as_flattened_mut(&mut self) -> &mut [T] { 4788622323SAlexandre Courbot self.flatten_mut() 4888622323SAlexandre Courbot } 4988622323SAlexandre Courbot } 50