1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::{into_iter, Matrix};

/// Calls a function eagerly on all elements of a matrix
/// 
/// This takes a function that accepts items from the matrix, and calls it on all elements of the matrix
pub trait ForEach<F> {
    fn for_each(self, f: F);
}

impl<T, F, const N: usize, const M: usize> ForEach<F> for Matrix<T, { N }, { M }>
where
    F: FnMut(T),
{
    fn for_each(self, mut f: F) {
        let Matrix(s) = self;

        let s = into_iter(s).map(into_iter);

        s.for_each(|s| unsafe { s.for_each(&mut f) });
    }
}

impl<'a, T, F, const N: usize, const M: usize> ForEach<F> for &'a mut Matrix<T, { N }, { M }>
where
    F: FnMut(&'a mut T),
{
    fn for_each(self, mut f: F) {
        let Matrix(s) = self;

        let s = s.iter_mut().map(|row| row.iter_mut());

        s.for_each(|s| unsafe { s.for_each(&mut f) });
    }
}

impl<'a, T, F, const N: usize, const M: usize> ForEach<F> for &'a Matrix<T, { N }, { M }>
where
    F: FnMut(&'a T),
{
    fn for_each(self, mut f: F) {
        let Matrix(s) = self;

        let s = s.iter().map(|row| row.iter());

        s.for_each(|s| unsafe { s.for_each(&mut f) });
    }
}