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§
Required Associated Types§
Required Methods§
Sourcefn as_slice(&self) -> &[Self::Element]
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);
Sourcefn as_mut_slice(&mut self) -> &mut [Self::Element]
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);
Sourcefn from_tuple(tuple: Self::Tuple) -> Self
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]);
Sourcefn into_tuple(self) -> Self::Tuple
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§
Sourcefn generate<F>(f: F) -> Selfwhere
Self: ArrayGenerate<F>,
fn generate<F>(f: F) -> Selfwhere
Self: ArrayGenerate<F>,
Builds an array by calling the provided function.
Type: F -> [T; N]
- when
N <= 1
this requiresF: FnOnce() -> T
- when
N > 1
this requiresF: 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]);
Sourcefn repeat<T: Clone>(x: T) -> Selfwhere
Self: ArrayRepeat<T>,
fn repeat<T: Clone>(x: T) -> Selfwhere
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
Sourcefn from_iter<I: IntoIterator>(it: I) -> Option<Self>where
Self: ArrayFromIter<I::IntoIter>,
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);
Sourcefn indices() -> Selfwhere
Self: ArrayIndices,
fn indices() -> Selfwhere
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]);
Sourcefn map<F>(self, f: F) -> <Self as ArrayMap<F>>::Outputwhere
Self: ArrayMap<F>,
fn map<F>(self, f: F) -> <Self as ArrayMap<F>>::Outputwhere
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 requiresF: FnOnce(T) -> U
- when
N > 1
this requiresF: FnMut(T) -> U
use arraytools::ArrayTools;
assert_eq!([1, 10, 100].map(|x| x + 10), [11, 20, 110]);
Sourcefn for_each<F>(self, f: F)where
Self: ArrayMap<F, OutputElement = ()>,
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 requiresF: FnOnce(T) -> ()
- when
N > 1
this requiresF: 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]);
Sourcefn zip<T>(self, other: T) -> <Self as ArrayZip<T>>::Outputwhere
Self: ArrayZip<T>,
fn zip<T>(self, other: T) -> <Self as ArrayZip<T>>::Outputwhere
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)]);
Sourcefn zip_with<T, F>(self, other: T, f: F) -> <Self as ArrayZipWith<T, F>>::Outputwhere
Self: ArrayZipWith<T, F>,
fn zip_with<T, F>(self, other: T, f: F) -> <Self as ArrayZipWith<T, F>>::Outputwhere
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 requiresF: FnOnce(T, U) -> V
- when
N > 1
this requiresF: FnMut(T, U) -> V
use arraytools::ArrayTools;
assert_eq!([10, 20, 30].zip_with([3, 2, 1], std::ops::Add::add), [13, 22, 31]);
Sourcefn as_ref_array<'a>(&'a self) -> <Self as ArrayAsRef<'a>>::Outputwhere
Self: ArrayAsRef<'a>,
fn as_ref_array<'a>(&'a self) -> <Self as ArrayAsRef<'a>>::Outputwhere
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]);
Sourcefn as_mut_array<'a>(&'a mut self) -> <Self as ArrayAsMut<'a>>::Outputwhere
Self: ArrayAsMut<'a>,
fn as_mut_array<'a>(&'a mut self) -> <Self as ArrayAsMut<'a>>::Outputwhere
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]);
Sourcefn push_back<U>(self, item: U) -> <Self as ArrayPush<U>>::Outputwhere
Self: ArrayPush<U>,
fn push_back<U>(self, item: U) -> <Self as ArrayPush<U>>::Outputwhere
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]);
Sourcefn push_front<U>(self, item: U) -> <Self as ArrayPush<U>>::Outputwhere
Self: ArrayPush<U>,
fn push_front<U>(self, item: U) -> <Self as ArrayPush<U>>::Outputwhere
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]);
Sourcefn pop_back<U>(self) -> (<Self as ArrayPop<U>>::Output, U)where
Self: ArrayPop<U>,
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));
Sourcefn pop_front<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>,
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.