Trait ArrayTools

Source
pub trait ArrayTools: Sized + Sealed {
    type Element;
    type Tuple;

    const LEN: usize;
Show 18 methods // Required methods fn as_slice(&self) -> &[Self::Element]; fn as_mut_slice(&mut self) -> &mut [Self::Element]; fn from_tuple(tuple: Self::Tuple) -> Self; fn into_tuple(self) -> Self::Tuple; // Provided methods fn generate<F>(f: F) -> Self where Self: ArrayGenerate<F> { ... } fn repeat<T: Clone>(x: T) -> Self where Self: ArrayRepeat<T> { ... } fn from_iter<I: IntoIterator>(it: I) -> Option<Self> where Self: ArrayFromIter<I::IntoIter> { ... } fn indices() -> Self where Self: ArrayIndices { ... } fn map<F>(self, f: F) -> <Self as ArrayMap<F>>::Output where Self: ArrayMap<F> { ... } fn for_each<F>(self, f: F) where Self: ArrayMap<F, OutputElement = ()> { ... } fn zip<T>(self, other: T) -> <Self as ArrayZip<T>>::Output where Self: ArrayZip<T> { ... } fn zip_with<T, F>( self, other: T, f: F, ) -> <Self as ArrayZipWith<T, F>>::Output where Self: ArrayZipWith<T, F> { ... } fn as_ref_array<'a>(&'a self) -> <Self as ArrayAsRef<'a>>::Output where Self: ArrayAsRef<'a> { ... } fn as_mut_array<'a>(&'a mut self) -> <Self as ArrayAsMut<'a>>::Output where Self: ArrayAsMut<'a> { ... } fn push_back<U>(self, item: U) -> <Self as ArrayPush<U>>::Output where Self: ArrayPush<U> { ... } fn push_front<U>(self, item: U) -> <Self as ArrayPush<U>>::Output where Self: ArrayPush<U> { ... } fn pop_back<U>(self) -> (<Self as ArrayPop<U>>::Output, U) where Self: ArrayPop<U> { ... } fn pop_front<U>(self) -> (<Self as ArrayPop<U>>::Output, U) where Self: ArrayPop<U> { ... }
}
Expand description

An extension trait for working with fixed-length arrays.

Use it with

use arraytools::ArrayTools;

For an overview, see the crate-level documentation.

(This trait is sealed; you are not allowed to implement it yourself.)

Required Associated Constants§

Source

const LEN: usize

The number of the elements in this array

use arraytools::ArrayTools;

assert_eq!(<[T; N] as ArrayTools>::LEN, N);

Required Associated Types§

Source

type Element

The type of the elements in this array

use arraytools::ArrayTools;

[T; N]: ArrayTools<Element = T>
Source

type Tuple

The homogeneous tuple type equivalent to this array type.

use arraytools::ArrayTools;

[T; 4]: ArrayTools<Tuple = (T, T, T, T)>

Required Methods§

Source

fn as_slice(&self) -> &[Self::Element]

Extracts a slice containing the entire array.

use arraytools::ArrayTools;

let array: [i32; 5] = [1, 2, 3, 4, 5];
let slice: &[i32] = array.as_slice();
assert_eq!(slice.len(), 5);
Source

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Extracts a mutable slice containing the entire array.

use arraytools::ArrayTools;

let mut array: [i32; 5] = [1, 2, 3, 4, 5];
let slice: &mut [i32] = array.as_mut_slice();
assert_eq!(slice.len(), 5);
Source

fn from_tuple(tuple: Self::Tuple) -> Self

Converts a homogeneous tuple into the equivalent array.

Type: (T, T, ..., T) -> [T; N]

use arraytools::ArrayTools;

assert_eq!(<[_; 3]>::from_tuple((1, 2, 3)), [1, 2, 3]);
Source

fn into_tuple(self) -> Self::Tuple

Converts this array into the equivalent homogeneous tuple.

Type: [T; N] -> (T, T, ..., T)

use arraytools::ArrayTools;

assert_eq!([1, 2, 3].into_tuple(), (1, 2, 3));

Provided Methods§

Source

fn generate<F>(f: F) -> Self
where Self: ArrayGenerate<F>,

Builds an array by calling the provided function.

Type: F -> [T; N]

  • when N <= 1 this requires F: FnOnce() -> T
  • when N > 1 this requires F: FnMut() -> T
use arraytools::ArrayTools;

let mut x = 1;
let array: [_; 5] = ArrayTools::generate(|| { x *= 2; x });
assert_eq!(array, [2, 4, 8, 16, 32]);
Source

fn repeat<T: Clone>(x: T) -> Self
where Self: ArrayRepeat<T>,

Builds an array by cloning the provided value.

Type: T -> [T; N]

use arraytools::ArrayTools;

let mut v = Vec::with_capacity(10);
v.push(42);
let array: [_; 5] = ArrayTools::repeat(v);
assert_eq!(array, [[42], [42], [42], [42], [42]]);
assert_eq!(array[3].capacity(), 1);
assert_eq!(array[4].capacity(), 10); // The last one is moved
Source

fn from_iter<I: IntoIterator>(it: I) -> Option<Self>
where Self: ArrayFromIter<I::IntoIter>,

Builds an array containing a prefix of the provided iterator, or returns None if it didn’t contain sufficient items.

Type: impl IntoIterator<Item = T> -> Option<[T; N]>

use arraytools::ArrayTools;

assert_eq!(<[i16; 4]>::from_iter(1..), Some([1, 2, 3, 4]));

assert_eq!(<[_; 4]>::from_iter(1..4), None);
assert_eq!(<[_; 4]>::from_iter(1..5), Some([1, 2, 3, 4]));
assert_eq!(<[_; 4]>::from_iter(1..6), Some([1, 2, 3, 4]));

assert_eq!(<[u8; 1]>::from_iter(Some(1)), Some([1]));
assert_eq!(<[u8; 1]>::from_iter(None), None);
Source

fn indices() -> Self
where Self: ArrayIndices,

Builds the array [0, 1, 2, ..., LEN-1].

Type: () -> [usize; N]

use arraytools::ArrayTools;

let array: [_; 5] = ArrayTools::indices();
assert_eq!(array, [0, 1, 2, 3, 4]);
Source

fn map<F>(self, f: F) -> <Self as ArrayMap<F>>::Output
where Self: ArrayMap<F>,

Builds a new array by applying the provided function to each element of this array.

Type: ([T; N], F) -> [U; N]

  • when N <= 1 this requires F: FnOnce(T) -> U
  • when N > 1 this requires F: FnMut(T) -> U
use arraytools::ArrayTools;

assert_eq!([1, 10, 100].map(|x| x + 10), [11, 20, 110]);
Source

fn for_each<F>(self, f: F)
where Self: ArrayMap<F, OutputElement = ()>,

Runs the provided function on each element of this array.

Type: ([T; N], F) -> ()

  • when N <= 1 this requires F: FnOnce(T) -> ()
  • when N > 1 this requires F: FnMut(T) -> ()
use arraytools::ArrayTools;

let mut array = [1, 10, 100];
array.as_mut_array().for_each(|x: &mut u8| *x += 10);
assert_eq!(array, [11, 20, 110]);
Source

fn zip<T>(self, other: T) -> <Self as ArrayZip<T>>::Output
where Self: ArrayZip<T>,

Combines two equal-length arrays into an array of tuples.

Type: ([T; N], [U; N]) -> [(T, U); N]

use arraytools::ArrayTools;

assert_eq!([10, 20, 30].zip([1.0, 2.0, 3.0]), [(10, 1.0), (20, 2.0), (30, 3.0)]);
Source

fn zip_with<T, F>(self, other: T, f: F) -> <Self as ArrayZipWith<T, F>>::Output
where Self: ArrayZipWith<T, F>,

Combines two equal-length arrays using the provided function.

Type: ([T; N], [U; N], F) -> [V; N]

  • when N <= 1 this requires F: FnOnce(T, U) -> V
  • when N > 1 this requires F: FnMut(T, U) -> V
use arraytools::ArrayTools;

assert_eq!([10, 20, 30].zip_with([3, 2, 1], std::ops::Add::add), [13, 22, 31]);
Source

fn as_ref_array<'a>(&'a self) -> <Self as ArrayAsRef<'a>>::Output
where Self: ArrayAsRef<'a>,

Builds an array of references to the elements of this array.

Type: &'a [T; N] -> [&'a T; N]

use arraytools::ArrayTools;

let array = &[1, 2, 3, 4, 5];
assert_eq!(array.as_ref_array(), [&1, &2, &3, &4, &5]);
Source

fn as_mut_array<'a>(&'a mut self) -> <Self as ArrayAsMut<'a>>::Output
where Self: ArrayAsMut<'a>,

Builds an array of mutable references to the elements of this array.

Type: &'a mut [T; N] -> [&'a mut T; N]

use arraytools::ArrayTools;

let array = &mut [1, 2, 3];
assert_eq!(array.as_ref_array(), [&mut 1, &mut 2, &mut 3]);
Source

fn push_back<U>(self, item: U) -> <Self as ArrayPush<U>>::Output
where Self: ArrayPush<U>,

Appends an item to this array, returning the new array

Type: ([T; N], T) -> [T; N+1]

use arraytools::ArrayTools;

assert_eq!([1, 2].push_back(10), [1, 2, 10]);
Source

fn push_front<U>(self, item: U) -> <Self as ArrayPush<U>>::Output
where Self: ArrayPush<U>,

Prepends an item to this array, returning the new array

Type: ([T; N], T) -> [T; N+1]

use arraytools::ArrayTools;

assert_eq!([1, 2].push_front(10), [10, 1, 2]);
Source

fn pop_back<U>(self) -> (<Self as ArrayPop<U>>::Output, U)
where Self: ArrayPop<U>,

Splits the last item off from this array, returning a tuple of an array of the other elements and the split-off item.

Type: [T; N+1] -> ([T; N], T)

use arraytools::ArrayTools;

assert_eq!([1, 2, 3].pop_back(), ([1, 2], 3));
Source

fn pop_front<U>(self) -> (<Self as ArrayPop<U>>::Output, U)
where Self: ArrayPop<U>,

Splits the first item off from this array, returning a tuple of an array of the other elements and the split-off item.

Type: [T; N+1] -> ([T; N], T)

use arraytools::ArrayTools;

assert_eq!([1, 2, 3].pop_front(), ([2, 3], 1));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> ArrayTools for [T; 0]

Source§

const LEN: usize = 0usize

Source§

type Element = T

Source§

type Tuple = ()

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 1]

Source§

const LEN: usize = 1usize

Source§

type Element = T

Source§

type Tuple = (T,)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 2]

Source§

const LEN: usize = 2usize

Source§

type Element = T

Source§

type Tuple = (T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 3]

Source§

const LEN: usize = 3usize

Source§

type Element = T

Source§

type Tuple = (T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 4]

Source§

const LEN: usize = 4usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 5]

Source§

const LEN: usize = 5usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 6]

Source§

const LEN: usize = 6usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 7]

Source§

const LEN: usize = 7usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 8]

Source§

const LEN: usize = 8usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 9]

Source§

const LEN: usize = 9usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 10]

Source§

const LEN: usize = 10usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 11]

Source§

const LEN: usize = 11usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 12]

Source§

const LEN: usize = 12usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 13]

Source§

const LEN: usize = 13usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 14]

Source§

const LEN: usize = 14usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 15]

Source§

const LEN: usize = 15usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 16]

Source§

const LEN: usize = 16usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 17]

Source§

const LEN: usize = 17usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 18]

Source§

const LEN: usize = 18usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 19]

Source§

const LEN: usize = 19usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 20]

Source§

const LEN: usize = 20usize

Source§

type Element = T

Source§

type Tuple = (T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T)

Source§

fn as_slice(&self) -> &[Self::Element]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Element]

Source§

fn from_tuple(tuple: Self::Tuple) -> Self

Source§

fn into_tuple(self) -> Self::Tuple

Source§

impl<T> ArrayTools for [T; 21]

Source§

impl<T> ArrayTools for [T; 22]

Source§

impl<T> ArrayTools for [T; 23]

Source§

impl<T> ArrayTools for [T; 24]

Source§

impl<T> ArrayTools for [T; 25]

Source§

impl<T> ArrayTools for [T; 26]

Source§

impl<T> ArrayTools for [T; 27]

Source§

impl<T> ArrayTools for [T; 28]

Source§

impl<T> ArrayTools for [T; 29]

Source§

impl<T> ArrayTools for [T; 30]

Source§

impl<T> ArrayTools for [T; 31]

Source§

impl<T> ArrayTools for [T; 32]

Implementors§