pub trait ArrayUnrolledExt<T, const N: usize>: Sized {
// Required methods
fn array_generate<F: FnMut(usize) -> T>(f: F) -> [T; N];
fn array_map<U, F: FnMut(T) -> U>(self, f: F) -> [U; N];
fn array_map_result<U, E, F: FnMut(T) -> Result<U, E>>(
self,
f: F,
) -> Result<[U; N], E>;
fn array_fold<U, F: FnMut(U, T) -> U>(self, init: U, f: F) -> U;
fn array_zip<T2>(self, arr2: [T2; N]) -> [(T, T2); N];
fn array_enumerate(self) -> [(usize, T); N];
fn array_as_ref(&self) -> [&T; N];
fn array_as_mut(&mut self) -> [&mut T; N];
// Provided method
fn array_for_each<F: FnMut(T)>(self, f: F) { ... }
}Expand description
Manually unrolled operations on arrays.
To ensure that operations are unrolled, consider annotating your closures with
#[inline(always)]. See the examples below for more details.
Required Methods§
Sourcefn array_generate<F: FnMut(usize) -> T>(f: F) -> [T; N]
fn array_generate<F: FnMut(usize) -> T>(f: F) -> [T; N]
Generate an array by filling the entries.
§Example
use vectoreyes::array_utils::*;
let arr = <[usize; 2]>::array_generate(#[inline(always)] |i| i + 1);
assert_eq!(arr, [1, 2]);Sourcefn array_map<U, F: FnMut(T) -> U>(self, f: F) -> [U; N]
fn array_map<U, F: FnMut(T) -> U>(self, f: F) -> [U; N]
Map over elements of an array.
§Example
use vectoreyes::array_utils::*;
let arr = [0, 1];
assert_eq!(arr.array_map(#[inline(always)] |x| x + 1), [1, 2]);Sourcefn array_map_result<U, E, F: FnMut(T) -> Result<U, E>>(
self,
f: F,
) -> Result<[U; N], E>
fn array_map_result<U, E, F: FnMut(T) -> Result<U, E>>( self, f: F, ) -> Result<[U; N], E>
Map over elements of an array, halting on the first error.
§Example
use vectoreyes::array_utils::*;
let arr = [0, 1];
assert_eq!(arr.array_map_result::<u32, u32, _>(#[inline(always)] |x| Err(x)), Err(0));
assert_eq!(arr.array_map_result::<u32, u32, _>(#[inline(always)] |x| Ok(x)), Ok([0, 1]));Sourcefn array_fold<U, F: FnMut(U, T) -> U>(self, init: U, f: F) -> U
fn array_fold<U, F: FnMut(U, T) -> U>(self, init: U, f: F) -> U
Fold over an array.
§Example
use vectoreyes::array_utils::*;
let out = [1, 2, 3].array_fold(0, #[inline(always)] |acu, x| acu + x);
assert_eq!(out, 6);Sourcefn array_zip<T2>(self, arr2: [T2; N]) -> [(T, T2); N]
fn array_zip<T2>(self, arr2: [T2; N]) -> [(T, T2); N]
Zip two arrays together.
§Example
use vectoreyes::array_utils::*;
assert_eq!(
['a', 'b', 'c'].array_zip(['x', 'y', 'z']),
[('a', 'x'), ('b', 'y'), ('c', 'z')]
);Sourcefn array_enumerate(self) -> [(usize, T); N]
fn array_enumerate(self) -> [(usize, T); N]
Produce an array where each element is a tuple containing each element’s index.
§Example
use vectoreyes::array_utils::*;
assert_eq!(
['a', 'b', 'c'].array_enumerate(),
[(0, 'a'), (1, 'b'), (2, 'c')],
);Sourcefn array_as_ref(&self) -> [&T; N]
fn array_as_ref(&self) -> [&T; N]
Produce an array containing references to the initial array.
Sourcefn array_as_mut(&mut self) -> [&mut T; N]
fn array_as_mut(&mut self) -> [&mut T; N]
Produce an array containing mutable references to the initial array.
Provided Methods§
Sourcefn array_for_each<F: FnMut(T)>(self, f: F)
fn array_for_each<F: FnMut(T)>(self, f: F)
Perform some computation over the elements of an array.
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.