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
// Things happen here, and they work.
//                       ,---.
//                       /    |
//                      /     |
//                     /      |
//                    /       |
//               ___,'        |
//             <  -'          :
//              `-.__..--'``-,_\_
//                 |o/ ` :,.)_`>
//                 :/ `     ||/)
//                 (_.).__,-` |\
//                 /( `.``   `| :
//                 \'`-.)  `  ; ;
//                 | `       /-<
//                 |     `  /   `.
// ,-_-..____     /|  `    :__..-'\
// ,'-.__\\  ``-./ :`      ;       \
//`\ `\  `\\  \ :  (   `  /  ,   `. \
//  \` \   \\   |  | `   :  :     .\ \
//   \ `\_  ))  :  ;     |  |      ): :
//  (`-.-'\ ||  |\ \   ` ;  ;       | |
//   \-_   `;;._   ( `  /  /_       | |
//    `-.-.// ,'`-._\__/_,'         ; |
//       \:: :     /     `     ,   /  |
//        || |    (        ,' /   /   |
//        ||                ,'   /    |

/// Prepend a new type into a cons list
pub trait ConsPrepend<T> {
    /// Result of prepend
    type Output;
    /// Prepend to runtime cons value
    fn prepend(self, t: T) -> Self::Output;
}

impl<T> ConsPrepend<T> for () {
    type Output = (T, Self);
    fn prepend(self, t: T) -> Self::Output {
        (t, self)
    }
}

impl<T, A, B> ConsPrepend<T> for (A, B) {
    type Output = (T, Self);
    fn prepend(self, t: T) -> Self::Output {
        (t, self)
    }
}

/// Prepend a new type into a cons list
pub trait ConsAppend<T> {
    /// Result of append
    type Output;
    /// Prepend to runtime cons value
    fn append(self, t: T) -> Self::Output;
}

impl<T> ConsAppend<T> for () {
    type Output = (T, Self);
    fn append(self, t: T) -> Self::Output {
        (t, ())
    }
}

impl<T, A, B: ConsAppend<T>> ConsAppend<T> for (A, B) {
    type Output = (A, <B as ConsAppend<T>>::Output);
    fn append(self, t: T) -> Self::Output {
        let (a, b) = self;
        (a, b.append(t))
    }
}

/// transform cons list into a flat tuple
pub trait ConsFlatten {
    /// Flattened tuple
    type Output;
    /// Flatten runtime cons value
    fn flatten(self) -> Self::Output;
}

impl ConsFlatten for () {
    type Output = ();
    fn flatten(self) -> Self::Output {
        self
    }
}

macro_rules! cons {
    () => (
        ()
    );
    ($head:tt) => (
        ($head, ())
    );
    ($head:tt, $($tail:tt),*) => (
        ($head, cons!($($tail),*))
    );
}

macro_rules! impl_flatten {
    ($($items:ident),*) => {
    #[allow(unused_parens)] // This is added because the nightly compiler complains
        impl<$($items),*> ConsFlatten for cons!($($items),*)
        {
            type Output = ($($items),*);
            fn flatten(self) -> Self::Output {
                #[allow(non_snake_case)]
                let cons!($($items),*) = self;
                ($($items),*)
            }
        }

        impl_flatten!(@ $($items),*);
    };
    (@ $head:ident, $($tail:ident),*) => {
        impl_flatten!($($tail),*);
    };
    (@ $head:ident) => {};
}

impl_flatten!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cons_macro() {
        #![allow(clippy::unit_cmp)]
        assert_eq!(cons!(), ());
        assert_eq!(cons!(1), (1, ()));
        assert_eq!(cons!(1, 2, 3, 4), (1, (2, (3, (4, ())))));
    }

    #[test]
    fn cons_prepend() {
        assert_eq!(().prepend(123), (123, ()));
        assert_eq!(
            cons!(1, 2, 3, 4, 5).prepend(123).prepend(15),
            cons!(15, 123, 1, 2, 3, 4, 5)
        );
    }

    #[test]
    fn cons_append() {
        assert_eq!(().append(123), (123, ()));
        assert_eq!(
            cons!(1, 2, 3, 4, 5).append(123).append(15),
            cons!(1, 2, 3, 4, 5, 123, 15)
        );
    }

    #[test]
    fn cons_flatten() {
        #![allow(clippy::unit_cmp)]
        assert_eq!(().flatten(), ());
        assert_eq!((1, ()).flatten(), 1);
        assert_eq!(cons!(1, 2, 3, 4, 5).flatten(), (1, 2, 3, 4, 5));
    }
}