ella_common/shape/
add.rs

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use super::{Const, Dyn, Shape};

pub trait NdimAdd<Other: Shape> {
    type Output: Shape;
}

impl<S: Shape> NdimAdd<S> for Const<0> {
    type Output = S;
}

macro_rules! impl_add_const {
    ($([$($t:tt)*])+) => {
        $(
            impl_add_const!($($t)*);
        )+
    };
    ($lhs:literal) => {
        impl_add_const!(Const<$lhs>, Dyn, Dyn);
    };
    ($lhs:literal, $rhs:literal) => {
        impl_add_const!(Const<$lhs>, Const<$rhs>, Const<{$lhs + $rhs}>);
    };
    ($lhs:literal, $rhs:literal, $out:ty) => {
        impl_add_const!(Const<$lhs>, Const<$rhs>, $out);
    };
    ($lhs:ty, $rhs:ty, $out:ty) => {
        impl NdimAdd<$rhs> for $lhs {
            type Output = $out;
        }
    };
}

impl_add_const!(
    [1, 0]
    [1, 1]
    [1, 2]
    [1, 3]
    [1, 4]
    [1, 5]
    [1, 6, Dyn]
    [1]

    [2, 0]
    [2, 1]
    [2, 2]
    [2, 3]
    [2, 4]
    [2, 5, Dyn]
    [2, 6, Dyn]
    [2]

    [3, 0]
    [3, 1]
    [3, 2]
    [3, 3]
    [3, 4, Dyn]
    [3, 5, Dyn]
    [3, 6, Dyn]
    [3]

    [4, 0]
    [4, 1]
    [4, 2]
    [4, 3, Dyn]
    [4, 4, Dyn]
    [4, 5, Dyn]
    [4, 6, Dyn]
    [4]

    [5, 0]
    [5, 1]
    [5, 2, Dyn]
    [5, 3, Dyn]
    [5, 4, Dyn]
    [5, 5, Dyn]
    [5, 6, Dyn]
    [5]

    [6, 0]
    [6, 1, Dyn]
    [6, 2, Dyn]
    [6, 3, Dyn]
    [6, 4, Dyn]
    [6, 5, Dyn]
    [6, 6, Dyn]
    [6]
);

impl<S: Shape> NdimAdd<S> for Dyn {
    type Output = Dyn;
}