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
//! An immutable list using [`Rc`](Rc).
//!
//! This list does not allow direct modification of its elements,
//! but modification of the [pointers](Rc) between them.
use alloc::rc::Rc;

/// A singly linked immutable list.
/// See the [module-level documentation](self) for more.
pub struct List<T> {
    head: Link<T>,
}

struct Node<T> {
    elem: T,
    next: Link<T>,
}

type Link<T> = Option<Rc<Node<T>>>;

impl<T> List<T> {
    /// Creates a new `List`.
    ///
    /// # Examples
    ///
    /// ```
    /// use cons_rs::immutable::List;
    /// 
    /// let list: List<i32> = List::new();
    /// ```
    #[inline]
    pub fn new() -> List<T> {
        List { head: None }
    }

    /// Prepends a given element to the list,
    /// returning a copy of the list with the added element.
    ///
    /// # Examples
    ///
    /// ```
    /// use cons_rs::immutable::List;
    /// 
    /// let list = List::new().prepend(1);
    ///
    /// assert_eq!(list.head(), Some(&1));
    /// ```
    pub fn prepend(&self, element: T) -> List<T> {
        List { head: Some(Rc::new(Node {
            elem: element,
            next: self.head.clone()
        }))}
    }

    /// Returns a copy of the list with the first element removed.
    ///
    /// # Examples
    ///
    /// ```
    /// use cons_rs::immutable::List;
    /// 
    /// let list = List::new().prepend(1);
    /// assert_eq!(list.tail().head(), None);
    ///
    /// let list = List::new().prepend(1).prepend(2);
    /// assert_eq!(list.tail().head(), Some(&1));
    /// ```
    pub fn tail(&self) -> List<T> {
        List {
            head: self.head.as_ref().and_then(|node| node.next.clone()) 
        }
    }

    /// Returns a reference to the first element in the list,
    /// if it exists.
    ///
    /// # Examples
    ///
    /// ```
    /// use cons_rs::immutable::List;
    /// 
    /// let list = List::new().prepend(1);
    ///
    /// assert_eq!(list.head(), Some(&1));
    /// ```
    pub fn head(&self) -> Option<&T> {
        self.head.as_ref().map(|node| &node.elem)
    }

    /// Creates an [iterator that yields references](Iter)
    /// to all the elements in the list.
    ///
    /// # Examples
    ///
    /// ```
    /// use cons_rs::immutable::List;
    /// 
    /// let list = List::new().prepend(1).prepend(2);
    ///
    /// let mut iter = list.iter();
    ///
    /// assert_eq!(iter.next(), Some(&2));
    /// assert_eq!(iter.next(), Some(&1));
    /// assert_eq!(iter.next(), None);
    /// ```
    pub fn iter(&self) -> Iter<'_, T> {
        Iter { next: self.head.as_deref() }
    }
}

impl<T> Default for List<T> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Drop for List<T> {
    fn drop(&mut self) {
        let mut head = self.head.take();
        while let Some(node) = head {
            if let Ok(mut node) = Rc::try_unwrap(node) {
                head = node.next.take();
            } else {
                break;
            }
        }
    }
}

/// An [iterator](Iterator) that yields references
/// to all the elements in a list.
pub struct Iter<'a, T> {
    next: Option<&'a Node<T>>
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.next.map(|node| {
            self.next = node.next.as_deref();
            &node.elem
        })
    }
}

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

    #[test]
    fn head_tail_prepend() {
        let list = List::new();

        let list = list.prepend(1).prepend(2);
        assert_eq!(list.head(), Some(&2));

        let list = list.tail();
        assert_eq!(list.head(), Some(&1));

        let list = list.tail();
        assert_eq!(list.head(), None);
        
        let list = list.tail();
        assert_eq!(list.head(), None);
    }

    #[test]
    fn iter() {
        let list = List::new().prepend(1).prepend(2);

        let mut iter = list.iter();
        
        assert_eq!(iter.next(), Some(&2));
        assert_eq!(iter.next(), Some(&1));
        assert_eq!(iter.next(), None);
    }
}