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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use Len;
use seq;
/// TODO currently structs are restricted to one type of chain impl
/// so you can't end up with mulitple types of cascades on the same type
/// this is desireable to reduce verbosity (which there is already too much of IMO)
/// this can be simplified by supporting sequences:
/// TODO often we don't want to be able to transform inputs -> outputs during the chain
/// it would be helpful for the user to be able to implement a Sequence<N> wrapper
/// this would wrap the Chain<N> implementation, forcing its `In` and `Out` to be the same value
const _TODO: = ;
/// Require all Length::Len types to be `L<const N: usize>` so that the InRange traits can be
/// implemented with the same marker type. This is a workaround to the rust compiler blindspot
/// which doesn't recognize certain non-overlapping trait impls and throws a compiler error.
///
/// Fails: `impl<T: Length<Len = L<N>>> InRange<I> for T {}`
/// Fails: `impl<T: Length> InRange<I, T::Len> for T {}`
/// Succeeds: `impl<T: Length<Len = L<N>>> InRange<I, L<N>> for T {}`
///
/// Even though there's no way anything can impl Len more than once.
// TODO I really hate this L<N> requirement, but we seem to need it to get around rust's
// compiler bug where non-overlapping trait impls are detected as overlapping, when
// using associated type equality as an impl condition
;
// TODO currently an annoying limitation is the hardcoded limit to how many things can be chained
// realistically it's not such a problem, since nobody's gonna implement more than 32 chains manually
// but if they do it via macro, it could become a limitation, but this is the best way I found so far
// add in-range marker trait impls for anything with up to length 32 (0..31 addressable)
seq!;
// type gymnastics so that the input of the next link is the output of the previous one
seq!;
// support chaining from 0..N for any T: Length<Len = L<N>> that has a Link at `N - 1`
// use sealed::Len;
// use seq_macro::seq;
// /// TODO currently structs are restricted to one type of chain impl
// /// so you can't end up with mulitple types of cascades on the same type
// /// this is desireable to reduce verbosity (which there is already too much of IMO)
// /// this can be simplified by supporting sequences:
// /// TODO often we don't want to be able to transform inputs -> outputs during the chain
// /// it would be helpful for the user to be able to implement a Sequence<N> wrapper
// /// this would wrap the Chain<N> implementation, forcing its `In` and `Out` to be the same value
// const _TODO: () = ();
// /// Require all Length::Len types to be `L<const N: usize>` so that the InRange traits can be
// /// implemented with the same marker type. This is a workaround to the rust compiler blindspot
// /// which doesn't recognize certain non-overlapping trait impls and throws a compiler error.
// ///
// /// Fails: `impl<T: Length<Len = L<N>>> InRange<I> for T {}`
// /// Fails: `impl<T: Length> InRange<I, T::Len> for T {}`
// /// Succeeds: `impl<T: Length<Len = L<N>>> InRange<I, L<N>> for T {}`
// ///
// /// Even though there's no way anything can impl Len more than once.
// mod sealed {
// pub trait Len {
// const LEN: usize;
// }
// }
// // TODO I really hate this L<N> requirement, but we seem to need it to get around rust's
// // compiler bug where non-overlapping trait impls are detected as overlapping, when
// // using associated type equality as an impl condition
// pub struct L<const N: usize>;
// impl<const N: usize> Len for L<N> {
// const LEN: usize = N;
// }
// pub trait Length {
// type Len: sealed::Len;
// fn len() -> usize {
// <Self::Len as Len>::LEN
// }
// }
// pub trait InRange<const N: usize, L>: Length {}
// pub trait Chain<const N: usize>
// where
// Self: Length,
// Self: InRange<N, <Self as Length>::Len>,
// {
// type In<'a>;
// type Out<'a>;
// fn chain(input: Self::In<'_>) -> Self::Out<'_>;
// }
// pub trait Link<const N: usize> {
// type In<'a>;
// type Out<'a>;
// fn link<'a>(input: Self::In<'a>) -> Self::Out<'a>;
// }
// impl<T: Chain<0>> Link<1> for T {
// type In<'a> = <T as Chain<0>>::In<'a>;
// type Out<'a> = <T as Chain<0>>::Out<'a>;
// fn link(input: Self::In<'_>) -> Self::Out<'_> {
// return <T as Chain<0>>::chain(input);
// }
// }
// // TODO currently an annoying limitation is the hardcoded limit to how many things can be chained
// // realistically it's not such a problem, since nobody's gonna implement more than 32 chains manually
// // but if they do it via macro, it could become a limitation, but this is the best way I found so far
// // add in-range marker trait impls for anything with up to length 32 (0..31 addressable)
// seq!(N in 1..=32 {
// seq!(I in 0..N {
// impl<T: Length<Len = L<N>>> InRange<I, L<N>> for T {}
// });
// });
// // type gymnastics so that the input of the next link is the output of the previous one
// seq!(N in 2..=32 {
// impl<T> Link<N> for T
// where
// T: Chain<0>,
// for<'a> T: Link<{N - 1}, In<'a> = <T as Chain<0>>::In<'a>>,
// for<'a> T: Chain<{N - 1}, In<'a> = <T as Link<{N - 1}>>::Out<'a>>,
// {
// type In<'a> = <T as Chain<0>>::In<'a>;
// type Out<'a> = <T as Chain<{N - 1}>>::Out<'a>;
// fn link(input: Self::In<'_>) -> Self::Out<'_> {
// let out = <T as Link<{N - 1}>>::link(input);
// return <T as Chain<{N - 1}>>::chain(out);
// }
// }
// });
// // support chaining from 0..N for any T: Length<Len = L<N>> that has a Link at `N - 1`
// pub trait Cascade {
// type In<'a>;
// type Out<'a>;
// fn cascade(input: Self::In<'_>) -> Self::Out<'_>;
// }
// impl<const N: usize, T: Link<N> + Length<Len = L<N>>> Cascade for T {
// type In<'a> = T::In<'a>;
// type Out<'a> = T::Out<'a>;
// fn cascade(input: Self::In<'_>) -> Self::Out<'_> {
// return <T as Link::<N>>::link(input);
// }
// }