Trait vectoreyes::array_utils::ArrayUnrolledExt
source · [−]pub trait ArrayUnrolledExt<T, const N: usize>: Sized {
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_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
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]);
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]);
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);
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')]
);
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.