File: slice.rs

package info (click to toggle)
linux 6.19~rc5-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,759,136 kB
  • sloc: ansic: 27,000,811; asm: 273,431; sh: 151,093; python: 81,275; makefile: 58,528; perl: 34,311; xml: 21,064; cpp: 5,984; yacc: 4,841; lex: 2,901; awk: 1,707; sed: 30; ruby: 25
file content (49 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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`]: https://doc.rust-lang.org/std/primitive.slice.html#method.as_flattened
/// [`as_flattened_mut`]: https://doc.rust-lang.org/std/primitive.slice.html#method.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`]: https://doc.rust-lang.org/std/primitive.slice.html#method.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`]: https://doc.rust-lang.org/std/primitive.slice.html#method.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()
    }
}