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
48
49
50
51
52
53
54
55
56
57
58
59
use crate::{ForEach, Get, ReadExtent};
use building_blocks_core::prelude::*;
use core::iter::{once, Once};
pub struct Func<F>(pub F);
impl<F, T, Coord> Get<Coord> for Func<F>
where
F: Fn(Coord) -> T,
{
type Item = T;
fn get(&self, c: Coord) -> T {
(self.0)(c)
}
}
impl<F, N, T> ForEach<N, PointN<N>> for Func<F>
where
F: Fn(PointN<N>) -> T,
PointN<N>: IntegerPoint<N>,
{
type Item = T;
fn for_each(&self, extent: &ExtentN<N>, mut f: impl FnMut(PointN<N>, Self::Item)) {
for p in extent.iter_points() {
f(p, (self.0)(p))
}
}
}
impl<'a, F, N, T> ReadExtent<'a, N> for Func<F>
where
F: 'a + Fn(PointN<N>) -> T,
PointN<N>: IntegerPoint<N>,
{
type Src = &'a F;
type SrcIter = Once<(ExtentN<N>, Self::Src)>;
fn read_extent(&'a self, extent: &ExtentN<N>) -> Self::SrcIter {
once((*extent, &self.0))
}
}