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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Type-level metaprogramming traits that allow the library to validate certain invariants at compile
//! time.
//!
//! This is broadly adapted from Lloyd Chan's excellent article [Gentle Intro to Type-level
//! Recursion in Rust][1]. However, since it's only used to enforce invariants, the resulting
//! heterogeneous list type doesn't contain any actual storage; this means that, at least in
//! theory, the compiler should be able to optimize it out completely.
//!
//! The meat of the module is found in the [`TypeList`](trait.TypeList.html),
//! [`Consume`](trait.Consume.html), and [`ConsumeMultiple`](trait.ConsumeMultiple.html) traits.
//! `TypeList`s are `cons`-style singly linked lists expressed in the type system;
//! the trait is implemented by [`TypeCons`](struct.TypeCons.html) and [`Nil`](enum.Nil.html).
//! As mentioned above, the types are effectively empty, and since `Nil` is an empty enum, cannot
//! even be constructed.
//!
//! The way this is used in the library is to indicate what types are available for Systems to
//! access within a World.
//!
//! # Examples
//!
//! `TypeList`s are constructed from lisp-style `cons` cells, terminating with `Nil`.
//! ```
//! # use ecstatic::typelist::*;
//! type AvailableTypes = TypeCons<f64, TypeCons<u32, TypeCons<String, Nil>>>;
//! ```
//!
//! The [`tlist!`](../macro.tlist.html) macro is provided to make writing these types easier and
//! less verbose.
//! ```
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! type AvailableTypes = tlist![f64, u32, String];
//! ```
//!
//! In this example, `do_stuff()` will take an argument of type `f64`, `u32`, or `String`. `I` is a
//! type parameter used by `Consume`; it should be left up to the type checker to infer. It's kind
//! of a bummer that this has to leak into the public interface, but that's the way it is.
//! ```
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! type AvailableTypes = tlist![f64, u32, String];
//! fn do_stuff<T, I>(t: T) where AvailableTypes: Consume<T, I> {
//! // Do something with `t`
//! }
//! do_stuff(25.0f64);
//! do_stuff(42u32);
//! do_stuff(String::from("Hello!"));
//! ```
//!
//! Calling `do_struff()` with types that are not in `AvailableTypes` will fail to type check.
//! ```compile_fail
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! struct Whatever {
//! x: f32,
//! y: f32,
//! }
//! type AvailableTypes = tlist![f64, u32, String];
//! fn do_stuff<T, I>(t: T) where AvailableTypes: Consume<T, I> {
//! // Do something with `t`
//! }
//! do_stuff(Whatever { x: 1.0, y: 3.0 });
//! ```
//!
//! Unfortunately, the error messages you get from the type checker failing are not particularly
//! helpful. For instance, in the example above, you will get something like the following:
//!
//! ```text
//! error[E0277]: the trait bound `main::ecstatic::typelist::Nil: main::ecstatic::typelist::Consume<main::Whatever, _>` is not satisfied
//! --> src/lib.rs:75:1
//! |
//! 14 | do_stuff(Whatever { 1.0, 3.0 });
//! | ^^^^^^^^ the trait `main::ecstatic::typelist::Consume<main::Whatever, _>` is not implemented for `main::ecstatic::typelist::Nil`
//! |
//! ```
//!
//! Not the greatest indicator of what the actual problem is.
//!
//! There is also a trait, `ConsumeMultiple`, that takes a `TypeList` as its type parameter (along
//! with a similar "Index" type that you should let the compiler infer, like with `Consume`).
//!
//! ```
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! type AvailableTypes = tlist![f64, u32, String];
//! fn do_stuff<T, I>() where AvailableTypes: ConsumeMultiple<T, I> {
//! // Do something
//! }
//! do_stuff::<tlist![f64, u32], _>();
//! ```
//!
//! This similarly will fail to type check if not all of the types are available in the source list.
//! ```compile_fail
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! type AvailableTypes = tlist![f64, u32, String];
//! fn do_stuff<T, I>() where AvailableTypes: ConsumeMultiple<T, I> {
//! // Do something
//! }
//! do_stuff::<tlist![f64, u32, &str], _>();
//! ```
//!
//! Importantly, `Consume<T, I>` removes *all* instances of `T` from the source list; this allows
//! us to write generic functions over `T`, `U` such that `T != U` (!).
//!
//! ```
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! fn do_stuff<T, U, I>() where tlist![T, U]: ConsumeMultiple<tlist![T, U], I> {
//! // Do something
//! }
//! do_stuff::<u32, f64, _>();
//! ```
//!
//! ```compile_fail
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! fn do_stuff<T, U, I>() where tlist![T, U]: ConsumeMultiple<tlist![T, U], I> {
//! // Do something
//! }
//!
//! // Using the same type for `T` and `U` causes a compilation error along the lines of the
//! // following:
//! //
//! // error[E0282]: type annotations needed
//! // --> src/ecs.rs:147:1
//! // |
//! // 8 | do_stuff::<u32, u32, _>();
//! // | ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `IHEAD`
//! do_stuff::<u32, u32, _>();
//! ```
//!
//! There is also a trait called `IntoTypeList` that allows easy conversion from tuples (up to
//! length 32) to `TypeList`.
//! ```
//! # #[macro_use] extern crate ecstatic;
//! # use ecstatic::typelist::*;
//! type AvailableTypes = tlist![f64, u32, String];
//! fn do_stuff<T, U, I>()
//! where
//! T: IntoTypeList<Type=U>,
//! // For some reason we still need to put a trait bound on `U`, even though the associated
//! // type is constrained in `IntoTypeList`
//! U: TypeList,
//! AvailableTypes: ConsumeMultiple<U, I>
//! {
//! // Do something
//! }
//! do_stuff::<(String, f64), _, _>();
//! ```
//!
//!
//! [1]: https://beachape.com/blog/2017/03/12/gentle-intro-to-type-level-recursion-in-Rust-from-zero-to-frunk-hlist-sculpting/
use PhantomData;
/// The empty list.
/// A cons cell
/// Trait implemented by `Nil` and `TypeCons`
/// Index struct for `Consume` that indicates `T` hasn't been found in the list yet.
;
/// Index for `Consume` that indicates a type has been found.
/// Generically append `T` to the end of a `TypeList`.
/// Removes all instances of `T`, leaving `Self::Remainder`. `INDEX` must be inferred.
///
/// See the [module-level documentation](index.html#examples) for examples.
/// Remove multiple elements, leaving `Self::Remainder`. `INDICES` must be inferred.
///
/// See the [module-level documentation](index.html#examples) for examples.
/// Easy conversion into `TypeList`.
// TypeLists are trivially convertible to TypeLists.
/// Helper macro for writing `TypeList`s.
// Recursive macro to implement IntoTypeList for tuples up to length 32
;
// Produce the actual impl for the tuple represented by $t1, then move $t2 into the tuple and
// recursively call impl_into_type_list
=> ;
// Entry point
=> ;
}
impl_into_type_list!;