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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! HList provides many operations to create and manipulate heterogenous lists (HLists) whose length
//! and element types are known at compile-time. HLists can be used to implement records, variants,
//! type-indexed products (TIP), type-indexed co-products (TIC), or keyword arguments.



// =============
// === HList ===
// =============

/// Type of every `HList`.
pub trait HList = HasLength;

/// Empty `HList` value.
#[derive(Debug,Clone,Copy)]
pub struct Nil;

/// Non-empty `HList` with head and tail.
#[derive(Debug,Clone,Copy)]
#[allow(missing_docs)]
pub struct Cons<Head,Tail>(pub Head, pub Tail);



// === Smart Constructors ===

/// Creates new `HList` from the provided elements, similar to `vec!`. In order to provide type for
/// the list, use the `ty` macro. In order to pattern match on it, use the `pat` macro.
///
/// ```compile_fail
/// let HList::pat![t1,t2] : HList::ty![&str,usize] = HList::new!["hello",7];
/// ```
#[macro_export]
macro_rules! new {
    ($(,)*) => { $crate::Nil };
    ($t:expr $(,$($ts:expr),*)?) => {
        $crate::Cons($t,$crate::new!{$($($ts),*)?})
    }
}

/// Pattern matches on a `HList`. See docs of `new` to learn more.
#[macro_export]
macro_rules! pat {
    ($(,)*) => { $crate::Nil };
    ($t:pat $(,$($ts:pat),*)?) => {
        $crate::Cons($t,$crate::pat!{$($($ts),*)?})
    }
}

/// Smart `HList` type constructor. See docs of `new` to learn more.
#[macro_export]
macro_rules! ty {
    ($(,)*) => { $crate::Nil };
    ($t:ty $(,$($ts:ty),*)?) => {
        $crate::Cons<$t,$crate::ty!{$($($ts),*)?}>
    }
}



// ==============
// === Length ===
// ==============

/// Compile-time known length value.
#[allow(missing_docs)]
pub trait HasLength {
    const LEN : usize;
    fn len() -> usize {
        Self::LEN
    }
}

/// Compile-time known length value.
pub const fn len<T:HasLength>() -> usize {
    <T as HasLength>::LEN
}

impl                HasLength for Nil       { const LEN : usize = 0; }
impl<H,T:HasLength> HasLength for Cons<H,T> { const LEN : usize = 1 + len::<T>(); }



// ============
// === Head ===
// ============

/// Head element accessor.
#[allow(missing_docs)]
pub trait KnownHead {
    type Head;
}

/// Head element type accessor.
pub type Head<T> = <T as KnownHead>::Head;

/// Head element accessor.
#[allow(missing_docs)]
pub trait GetHead : KnownHead {
    fn head(&self) -> &Self::Head;
}

/// Mutable head element accessor.
#[allow(missing_docs)]
pub trait GetHeadMut : KnownHead {
    fn head_mut(&mut self) -> &mut Self::Head;
}

/// Head element clone.
#[allow(missing_docs)]
pub trait GetHeadClone : KnownHead {
    fn head_clone(&self) -> Self::Head;
}

impl<T> GetHeadClone for T
where T:GetHead, Head<T>:Clone {
    default fn head_clone(&self) -> Self::Head {
        self.head().clone()
    }
}


// === Impls ===

impl<H,T> KnownHead for Cons<H,T> {
    type Head = H;
}

impl<H,T> GetHead for Cons<H,T> {
    fn head(&self) -> &Self::Head {
        &self.0
    }
}

impl<H,T> GetHeadMut for Cons<H,T> {
    fn head_mut(&mut self) -> &mut Self::Head {
        &mut self.0
    }
}



// ============
// === Tail ===
// ============

/// Tail element accessor.
#[allow(missing_docs)]
pub trait KnownTail {
    type Tail;
}

/// Tail element type accessor.
pub type Tail<T> = <T as KnownTail>::Tail;

/// Tail element accessor.
#[allow(missing_docs)]
pub trait GetTail : KnownTail {
    fn tail(&self) -> &Self::Tail;
}

/// Mutable tail element accessor.
#[allow(missing_docs)]
pub trait GetTailMut : KnownTail {
    fn tail_mut(&mut self) -> &mut Self::Tail;
}

/// Tail element clone.
#[allow(missing_docs)]
pub trait GetTailClone : KnownTail {
    fn tail_clone(&self) -> Self::Tail;
}

impl<T> GetTailClone for T
    where T:GetTail, Tail<T>:Clone {
    default fn tail_clone(&self) -> Self::Tail {
        self.tail().clone()
    }
}


// === Impls ===

impl<H,T> KnownTail for Cons<H,T> {
    type Tail = T;
}

impl<H,T> GetTail for Cons<H,T> {
    fn tail(&self) -> &Self::Tail {
        &self.1
    }
}

impl<H,T> GetTailMut for Cons<H,T> {
    fn tail_mut(&mut self) -> &mut Self::Tail {
        &mut self.1
    }
}



// ============
// === Last ===
// ============

/// Last element accessor.
#[allow(missing_docs)]
pub trait KnownLast {
    type Last;
}

/// Last element type accessor.
pub type Last<T> = <T as KnownLast>::Last;

/// Last element accessor.
#[allow(missing_docs)]
pub trait GetLast : KnownLast {
    fn last(&self) -> &Self::Last;
}

/// Mutable last element accessor.
#[allow(missing_docs)]
pub trait GetLastMut : KnownLast {
    fn last_mut(&mut self) -> &mut Self::Last;
}

/// Last element clone.
#[allow(missing_docs)]
pub trait GetLastClone : KnownLast {
    fn last_clone(&self) -> Self::Last;
}

impl<T> GetLastClone for T
    where T:GetLast, Last<T>:Clone {
    default fn last_clone(&self) -> Self::Last {
        self.last().clone()
    }
}


// === Impls ===

impl<H>             KnownLast for Cons<H,Nil> { type Last = H; }
impl<H,T:KnownLast> KnownLast for Cons<H,T>   { type Last = Last<T>; }

impl<H> GetLast for Cons<H,Nil> {
    fn last(&self) -> &Self::Last {
        &self.0
    }
}

impl<H> GetLastMut for Cons<H,Nil> {
    fn last_mut(&mut self) -> &mut Self::Last {
        &mut self.0
    }
}

impl<H,T:GetLast> GetLast for Cons<H,T> {
    fn last(&self) -> &Self::Last {
        self.tail().last()
    }
}

impl<H,T:GetLastMut> GetLastMut for Cons<H,T> {
    fn last_mut(&mut self) -> &mut Self::Last {
        self.tail_mut().last_mut()
    }
}




// ============
// === Init ===
// ============

/// Init elements accessor (all but last).
#[allow(missing_docs)]
pub trait KnownInit {
    type Init;
}

/// Init elements type accessor.
pub type Init<T> = <T as KnownInit>::Init;

/// Init element clone.
#[allow(missing_docs)]
pub trait GetInitClone : KnownInit {
    fn init_clone(&self) -> Self::Init;
}


// === Impls ===

impl<H>             KnownInit for Cons<H,Nil> { type Init = Nil; }
impl<H,T:KnownInit> KnownInit for Cons<H,T>   { type Init = Cons<H,Init<T>>; }

impl<H> GetInitClone for Cons<H,Nil> {
    fn init_clone(&self) -> Self::Init {
        Nil
    }
}

impl<H:Clone,T:GetInitClone> GetInitClone for Cons<H,T> {
    fn init_clone(&self) -> Self::Init {
        Cons(self.head().clone(),self.tail().init_clone())
    }
}



// ================
// === PushBack ===
// ================

// TODO: Consider implementing PushBack for everything that converts to and from HList.

/// Add a new element to the back of the list.
#[allow(missing_docs)]
pub trait PushBack<T> : Sized {
    type Output : KnownLast<Last=T> + KnownInit<Init=Self>;
    fn push_back(self,t:T) -> Self::Output;
}

impl<X> PushBack<X> for Nil {
    type Output = Cons<X,Nil>;
    #[inline(always)]
    fn push_back(self,x:X) -> Self::Output {
        Cons(x,Nil)
    }
}

impl<X,H,T> PushBack<X> for Cons<H,T>
    where T:PushBack<X> {
    type Output = Cons<H,<T as PushBack<X>>::Output>;
    #[inline(always)]
    fn push_back(self,x:X) -> Self::Output {
        let Cons(head,tail) = self;
        Cons(head,tail.push_back(x))
    }
}



// ===============
// === PopBack ===
// ===============

// TODO: Consider implementing PopBack for everything that converts to and from HList.

/// Remove the last element of the list and return it and the new list.
#[allow(missing_docs)]
pub trait PopBack : KnownLast + KnownInit {
    fn pop_back(self) -> (Self::Last,Self::Init);
}

impl<H> PopBack for Cons<H,Nil> {
    fn pop_back(self) -> (Self::Last,Self::Init) {
        (self.0,Nil)
    }
}

impl<H,T> PopBack for Cons<H,T>
where T:PopBack {
    fn pop_back(self) -> (Self::Last,Self::Init) {
        let (last,tail) = self.1.pop_back();
        (last,Cons(self.0,tail))
    }
}