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
pub trait ArrayMap<X, Y, T> {
fn map<F: Fn(&X) -> Y>(&self, f: F) -> T;
}
macro_rules! map_inner {
{$f:ident, $s:expr, []; $n:expr, $($ns:expr),*,} => {
map_inner!($f, $s, [$f(&$s[$n])]; $($ns),*,)
};
{$f:ident, $s:expr, [$($t:tt)*]; $n:expr, $($ns:expr),*,} => {
map_inner!($f, $s, [$f(&$s[$n]), $($t)*]; $($ns),*,)
};
{$f:ident, $s:expr, [$($t:tt)*]; $n:expr, } => {
map_inner!($f, $s, [$f(&$s[$n]), $($t)*])
};
{$f:ident, $s:expr, $t:expr} => { $t };
}
macro_rules! map_impl {
{$n:expr, $($ns:expr),*} => {
impl<U, V> ArrayMap<U, V, [V; $n]> for [U; $n] {
fn map<F: Fn(&U) -> V>(&self, f: F) -> [V; $n] {
map_inner!(f, self, []; $($ns),*,)
}
}
map_impl!{$($ns),*}
};
{$n:expr} => { }
}
impl<U, V> ArrayMap<U, V, [V; 0]> for [U; 0] {
fn map<F: Fn(&U) -> V>(&self, _: F) -> [V; 0] { [] }
}
map_impl!{32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
#[cfg(test)]
mod tests {
use ArrayMap;
#[test]
fn empty_array() {
let x : [u8; 0] = [];
assert_eq!(x, x.map(|_| unreachable!()));
}
}