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
/*!
Contains type-level lists,and related items
*/

use std_::{
    fmt::{self, Debug},
    marker::PhantomData,
};

use crate::type_level::collection_traits::{
    Append, AppendOut, Flatten, PushBack, PushBackOut, ToTList, ToTListOut,
};

#[cfg(test)]
mod tests;

////////////////////////////////////////////////////////////////////////////////

/// A type-level non-empty list.
pub struct TList<Curr, Rem>(PhantomData<fn() -> (Curr, Rem)>);

/// A type-level empty list.
#[derive(Debug, Copy, Clone)]
pub struct TNil;

////////////////////////////////////////////////////////////////////////////////

impl<Curr, Rem> core_extensions::ConstDefault for TList<Curr, Rem> {
    const DEFAULT: Self = TList(PhantomData);
}

impl<Curr, Rem> Debug for TList<Curr, Rem> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TList").finish()
    }
}

impl<Curr, Rem> Copy for TList<Curr, Rem> {}

impl<Curr, Rem> Clone for TList<Curr, Rem> {
    fn clone(&self) -> Self {
        TList::NEW
    }
}

impl<Curr, Rem> TList<Curr, Rem> {
    /// Constructs this list.
    pub const NEW: Self = TList(PhantomData);
}

/////////////////////////////////////////////////////////////////////////////////

impl<T, Rem> ToTList for TList<T, Rem> {
    type Output = Self;
}

impl ToTList for TNil {
    type Output = Self;
}

/////////////////////////////////////////////////////////////////////////////////

impl core_extensions::ConstDefault for TNil {
    const DEFAULT: Self = TNil;
}

impl TNil {
    /// Constructs this empty list.
    pub const NEW: Self = TNil;
}

////////////////////////////////////////////////////////////////////////////////

impl<Current, Rem, Elem> PushBack<Elem> for TList<Current, Rem>
where
    Rem: PushBack<Elem>,
{
    type Output = TList<Current, PushBackOut<Rem, Elem>>;
}

impl<Elem> PushBack<Elem> for TNil {
    type Output = TList<Elem, TNil>;
}

////////////////////////////////////////////////////////////////////////////////

impl<T, Rem, T2, Rem2> Append<TList<T2, Rem2>> for TList<T, Rem>
where
    Rem: Append<TList<T2, Rem2>>,
{
    type Output = TList<T, AppendOut<Rem, TList<T2, Rem2>>>;
}

impl<T, Rem> Append<TNil> for TList<T, Rem> {
    type Output = TList<T, Rem>;
}

impl<T, Rem> Append<TList<T, Rem>> for TNil {
    type Output = TList<T, Rem>;
}

impl Append<TNil> for TNil {
    type Output = TNil;
}

////////////////////////////////////////////////////////////////////////////////

impl Flatten for TNil {
    type Output = TNil;
}
impl<Curr, Rem, Out> Flatten for TList<Curr, Rem>
where
    (): FlattenOutImpl<Rem, Curr, Output = Out>,
{
    type Output = Out;
}

#[doc(hidden)]
pub trait FlattenOutImpl<Outer, Inner> {
    type Output;
}

impl<List> FlattenOutImpl<TNil, List> for ()
where
    List: ToTList,
{
    type Output = ToTListOut<List>;
}

impl<Curr, Rem, Out> FlattenOutImpl<TList<Curr, Rem>, TNil> for ()
where
    Curr: ToTList,
    (): FlattenOutImpl<Rem, ToTListOut<Curr>, Output = Out>,
{
    type Output = Out;
}

impl<CurrI, RemI, CurrO, RemO, Out> FlattenOutImpl<TList<CurrO, RemO>, TList<CurrI, RemI>> for ()
where
    RemI: ToTList,
    (): FlattenOutImpl<TList<CurrO, RemO>, ToTListOut<RemI>, Output = Out>,
{
    type Output = TList<CurrI, Out>;
}