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
use crate::{Cons, HList, Nil};
/// Reverse elements of the heterogenous list.
pub trait Reverse: HList {
/// Type of new heterogenous list with the opposite order of elements.
type Output: HList;
/// Reverses elements of the heterogenous list.
///
/// New heterogenous list will contain all elements of the old one
/// with the opposite order.
///
/// # Examples
///
/// ```
/// use hlist2::{hlist, ops::Reverse};
///
/// let list = hlist![1, 2.0, true, "hello world"];
/// assert_eq!(list.reverse(), hlist!["hello world", true, 2.0, 1]);
/// ```
fn reverse(self) -> Self::Output;
}
impl<T> Reverse for T
where
T: Rewind<Nil>,
{
type Output = T::Output;
fn reverse(self) -> Self::Output {
self.rewind(Nil)
}
}
pub trait Rewind<Done>: HList
where
Done: HList,
{
type Output: HList;
fn rewind(self, done: Done) -> Self::Output;
}
impl<Done> Rewind<Done> for Nil
where
Done: HList,
{
type Output = Done;
fn rewind(self, done: Done) -> Self::Output {
done
}
}
impl<Done, Next, Tail> Rewind<Done> for Cons<Next, Tail>
where
Done: HList,
Tail: Rewind<Cons<Next, Done>>,
{
type Output = Tail::Output;
fn rewind(self, done: Done) -> Self::Output {
let Cons(next, tail) = self;
tail.rewind(Cons(next, done))
}
}