Trait SliceOfArrayExt

Source
pub trait SliceOfArrayExt {
    // Required methods
    fn flatten_ext(&self) -> &[Self::T];
    fn flatten_mut_ext(&mut self) -> &mut [Self::T];
}
Expand description

A helper extension trait for slices of arrays

Required Methods§

Source

fn flatten_ext(&self) -> &[Self::T]

Takes a &[[T; N]], and flattens it to a &[T].

§Panics

This panics if the length of the resulting slice would overflow a usize.

This is only possible when flattening a slice of arrays of zero-sized types, and thus tends to be irrelevant in practice. If size_of::<T>() > 0, this will never panic.

§Examples
use array_util::SliceOfArrayExt;

assert_eq!([[1, 2, 3], [4, 5, 6]].flatten_ext(), &[1, 2, 3, 4, 5, 6]);

assert_eq!(
    [[1, 2, 3], [4, 5, 6]].flatten_ext(),
    [[1, 2], [3, 4], [5, 6]].flatten_ext(),
);

let slice_of_empty_arrays: &[[i32; 0]] = &[[], [], [], [], []];
assert!(slice_of_empty_arrays.flatten_ext().is_empty());

let empty_slice_of_arrays: &[[u32; 10]] = &[];
assert!(empty_slice_of_arrays.flatten_ext().is_empty());
Source

fn flatten_mut_ext(&mut self) -> &mut [Self::T]

Takes a &mut [[T; N]], and flattens it to a &mut [T].

§Panics

This panics if the length of the resulting slice would overflow a usize.

This is only possible when flattening a slice of arrays of zero-sized types, and thus tends to be irrelevant in practice. If size_of::<T>() > 0, this will never panic.

§Examples
use array_util::SliceOfArrayExt;

fn add_5_to_all(slice: &mut [i32]) {
    for i in slice {
        *i += 5;
    }
}

let mut array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
add_5_to_all(array.flatten_mut_ext());
assert_eq!(array, [[6, 7, 8], [9, 10, 11], [12, 13, 14]]);

Implementations on Foreign Types§

Source§

impl<T, const N: usize> SliceOfArrayExt for [[T; N]]

Source§

fn flatten_ext(&self) -> &[Self::T]

Takes a &[[T; N]], and flattens it to a &[T].

§Panics

This panics if the length of the resulting slice would overflow a usize.

This is only possible when flattening a slice of arrays of zero-sized types, and thus tends to be irrelevant in practice. If size_of::<T>() > 0, this will never panic.

§Examples
use array_util::SliceOfArrayExt;

assert_eq!([[1, 2, 3], [4, 5, 6]].flatten_ext(), &[1, 2, 3, 4, 5, 6]);

assert_eq!(
    [[1, 2, 3], [4, 5, 6]].flatten_ext(),
    [[1, 2], [3, 4], [5, 6]].flatten_ext(),
);

let slice_of_empty_arrays: &[[i32; 0]] = &[[], [], [], [], []];
assert!(slice_of_empty_arrays.flatten_ext().is_empty());

let empty_slice_of_arrays: &[[u32; 10]] = &[];
assert!(empty_slice_of_arrays.flatten_ext().is_empty());
Source§

fn flatten_mut_ext(&mut self) -> &mut [Self::T]

Takes a &mut [[T; N]], and flattens it to a &mut [T].

§Panics

This panics if the length of the resulting slice would overflow a usize.

This is only possible when flattening a slice of arrays of zero-sized types, and thus tends to be irrelevant in practice. If size_of::<T>() > 0, this will never panic.

§Examples
use array_util::SliceOfArrayExt;

fn add_5_to_all(slice: &mut [i32]) {
    for i in slice {
        *i += 5;
    }
}

let mut array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
add_5_to_all(array.flatten_mut_ext());
assert_eq!(array, [[6, 7, 8], [9, 10, 11], [12, 13, 14]]);

Implementors§