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
use typenum::consts::{U1};
use std::ops::Add;
use super::ArrayLength;

/// Helper trait for `arr!` macro
pub trait AddLength<T, N: ArrayLength<T>>: ArrayLength<T> {
    type Output: ArrayLength<T>;
}

impl<T, N1, N2> AddLength<T, N2> for N1
    where N1: ArrayLength<T> + Add<N2>,
          N2: ArrayLength<T>,
          <N1 as Add<N2>>::Output: ArrayLength<T>
{
    type Output = <N1 as Add<N2>>::Output;
}

/// Helper type for `arr!` macro
pub type Inc<T, U> = <U as AddLength<T, U1>>::Output;

/// Proper implementation of `arr!`
#[macro_export]
macro_rules! arr_impl {
    ($T:ty; $N:ty, [$($x:expr),*], []) => ({
        use typenum::consts::U0;
        use generic_array::GenericArray;
        use generic_array::arr::Inc;
        GenericArray::<$T, $N>::from_slice(&[$($x),*])
    });
    ($T:ty; $N:ty, [], [$x1:expr]) => (
        arr_impl!($T; Inc<$T, $N>, [$x1], [])
    );
    ($T:ty; $N:ty, [], [$x1:expr, $($x:expr),+]) => (
        arr_impl!($T; Inc<$T, $N>, [$x1], [$($x),*])
    );
    ($T:ty; $N:ty, [$($y:expr),+], [$x1:expr]) => (
        arr_impl!($T; Inc<$T, $N>, [$($y),*, $x1], [])
    );
    ($T:ty; $N:ty, [$($y:expr),+], [$x1:expr, $($x:expr),+]) => (
        arr_impl!($T; Inc<$T, $N>, [$($y),*, $x1], [$($x),*])
    );
}

/// Macro allowing for easy generation of Generic Arrays.
#[macro_export]
macro_rules! arr {
    ($T:ty; $($x:expr),*) => (
        arr_impl!($T; U0, [], [$($x),*])
    );
    ($($x:expr,)*) => (arr![$($x),*])
}