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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use typenum::{Add1, B1, Length, TArr, ATerm, Len, Integer, Unsigned, U0};
use generic_array::{GenericArray, ArrayLength};
use core::ops::Add;
pub trait ToGA {
type Output;
fn to_ga() -> Self::Output;
}
impl ToGA for ATerm {
type Output = GenericArray<isize, U0>;
fn to_ga() -> Self::Output {
GenericArray::new()
}
}
impl<V, A> ToGA for TArr<V, A>
where V: Integer,
A: Len + ToGA,
<A as ToGA>::Output: AppendFront<isize>,
Length<A>: Add<B1>,
Add1<Length<A>>: Unsigned + ArrayLength<isize>
{
type Output = <<A as ToGA>::Output as AppendFront<isize>>::Output;
fn to_ga() -> Self::Output {
A::to_ga().append_front(V::to_isize())
}
}
pub trait AppendFront<T> {
type Output;
fn append_front(self, element: T) -> Self::Output;
}
impl<T, N> AppendFront<T> for GenericArray<T, N>
where T: Default,
N: Add<B1> + ArrayLength<T>,
Add1<N>: ArrayLength<T>
{
type Output = GenericArray<T, Add1<N>>;
fn append_front(self, element: T) -> Self::Output {
let mut a = GenericArray::new();
a[0] = element;
for (i, el) in self.into_iter().enumerate() {
a[i + 1] = el;
}
a
}
}
#[test]
fn test_array() {
use typenum::consts::*;
type A = tarr![P1, N3, P4];
let a = A::to_ga();
assert_eq!(a, arr![isize; 1, -3, 4]);
}