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
//! This crate defines a way to create compile-time heterogenous lists,
//! or lists consisting of multiple types.
//!
//! # Heterogenous lists
//!
//! This crate defines types for an empty list, [`Nil`],
//! and for pair of list head and its remainder, [`Cons`].
//! Heterogenous list consists of many conses contained recursively one in another,
//! and the last cons with the last element contains nil as the remainder.
//!
//! For example, heterogenous list of integer, double and bool can be represented as
//! `Cons(1, Cons(2.0, Cons(true, Nil)))` with type of `Cons<i32, Cons<f64, Cons<bool, Nil>>>`.
//!
//! # Recursive nature and behavior
//!
//! Such recursive nature of heterogenous list allows us to implement various traits recursively
//! and without any restrictions on the size of such list or types contained in it.
//! Unlike [tuples](prim@tuple), traits can be implemented for all heterogenous lists
//! and even for those which count of elements is bigger than 12, lack of which for tuples is a problem sometimes.
//!
//! All heterogenous lists implement [`HList`][hlist] trait, so it can be used in generics.
//! For example, this can be useful to bound generic type to be heterogenous list.
//!
//! To implement your trait for all heterogenous lists of any size,
//! first implement it on [`Nil`] type, which is [`HList`][hlist] too.
//! Then, implement your trait on [`Cons`] struct with head and tail generic types
//! where tail type is heterogenous list too (or which implement [`HList`][hlist] trait).
//!
//! Examples of these technique can be viewed in [`ops`] module, where
//! all the specific operations for all heterogenous list types are implemented.
//! For example, to append any value to the end of the list, use [`Append`][append] trait;
//! to prepend any value to the beginning of the list, use [`Prepend`][prepend] trait, and so on.
//!
//! [hlist]: trait@crate::HList
//! [append]: crate::ops::Append
//! [prepend]: crate::ops::Prepend
//!
//! # Constructing and destructing heterogenous lists
//!
//! But such recursive nature can be a problem when we try to name the type of heterogenous list
//! or use pattern matching with values of heterogenous lists.
//! To simplify creation of lists and naming of list types the crate defines two macros,
//! [`hlist!`] and [`HList!`].
//! The first one should be used for creation of heterogenous lists or for pattern matching,
//! while the second one should be used to name the type of heterogenous list.
//!
//! So instead of writing `Cons(1, Cons(2.0, Cons(true, Nil)))`
//! we can write more readable expression like `hlist![1, 2.0, true]`.
//!
//! To name the type of such list, we can write `HList![i32, f64, bool]`
//! instead of `Cons<i32, Cons<f64, Cons<bool, Nil>>>`.
//!
//! # Tuple compatibility
//!
//! Also this crate has a compatibility with [tuple](prim@tuple) types.
//! It implements conversion between heterogenous lists and their tuple forms
//! when tuple has length of 12 and less, and vise versa.
//!
//! # Features
//!
//! This crate uses **no unsafe code** to provide the same
//! safety guarantees the Rust programming language provides.
//!
//! This crate is `no_std`, so it can be used freely and with no fear in embedded environment.
pub use ;
/// Compile-time heterogenous list.
///
/// This trait is sealed and cannot be implemented outside of this crate.
/// It is implemented only for [`Cons`] and [`Nil`] structs.
/// Heterogenous list with length (count of elements) known at compile-time.
/// Macro creating heterogenous list values from list of expressions.
///
/// This macro supports trailing comma at the end of list of expressions.
///
/// # Examples
///
/// This macro can be used to create heterogenous list
/// as easily as tuple without cons-nil boilerplate:
///
/// ```
/// use hlist2::{hlist, Cons, Nil};
///
/// let list = hlist![1, 2.0, true];
/// assert_eq!(list, Cons(1, Cons(2.0, Cons(true, Nil))));
/// ```
///
/// Also it can be used in pattern matching.
/// For example, we can destruct heterogenous list to its values:
///
/// ```
/// use hlist2::hlist;
///
/// let hlist![a, b, c, d] = hlist![10, -15.0, "hello world", false];
/// assert_eq!((a, b, c, d), (10, -15.0, "hello world", false));
/// ```
/// Macro creating heterogenous list types from list of element types.
///
/// This macro supports trailing comma at the end of list of element types.
///
/// # Examples
///
/// This macro can be used to name heterogenous list type
/// as easily as tuple type without cons-nil boilerplate:
///
/// ```
/// use hlist2::{hlist, HList, Cons, Nil};
///
/// let list: HList![i32, f64, bool] = hlist![1, 2.0, true];
/// let list: Cons<i32, Cons<f64, Cons<bool, Nil>>> = list;
/// ```