1pub struct List<T> {
2 head: Link<T>,
3}
4
5type Link<T> = Option<Box<Node<T>>>;
6
7struct Node<T> {
8 elem: T,
9 next: Link<T>,
10}
11
12pub struct IntoIter<T>(List<T>);
13
14pub struct Iter<'a, T: 'a> {
15 next: Option<&'a Node<T>>,
16}
17
18pub struct IterMut<'a, T: 'a> {
19 next: Option<&'a mut Node<T>>,
20}
21
22impl<T> List<T> {
23
24 pub fn new() -> Self {
25 List { head: None }
26 }
27
28 pub fn push(&mut self, elem: T) {
29 let new_node = Box::new(Node {
30 elem: elem,
31 next: self.head.take(),
32 });
33
34 self.head = Some(new_node);
35 }
36
37 pub fn pop(&mut self) -> Option<T> {
38 self.head.take().map(|node| {
39 let node = *node;
40 self.head = node.next;
41 node.elem
42 })
43 }
44
45 pub fn peek(&self) -> Option<&T> {
46 self.head.as_ref().map(|node| {
47 &node.elem
48 })
49 }
50
51 pub fn peek_mut(&mut self) -> Option<&mut T> {
52 self.head.as_mut().map(|node| {
53 &mut node.elem
54 })
55 }
56
57 pub fn into_iter(self) -> IntoIter<T> {
58 IntoIter(self)
59 }
60
61 pub fn iter(&self) -> Iter<T> {
62 Iter { next: self.head.as_ref().map(|node| &**node) }
63 }
64
65 pub fn iter_mut(&mut self) -> IterMut<T> {
66 IterMut { next: self.head.as_mut().map(|node| &mut **node) }
67 }
68}
69
70impl<T> Drop for List<T> {
71 fn drop(&mut self) {
72 let mut cur_link = self.head.take();
73 while let Some(mut boxed_node) = cur_link {
74 cur_link = boxed_node.next.take();
75 }
76 }
77}
78
79impl<T> Iterator for IntoIter<T> {
80 type Item = T;
81 fn next(&mut self) -> Option<Self::Item> {
82 self.0.pop()
83 }
84}
85
86impl<'a, T> Iterator for Iter<'a, T> {
87 type Item = &'a T;
88
89 fn next(&mut self) -> Option<Self::Item> {
90 self.next.map(|node| {
91 self.next = node.next.as_ref().map(|node| &**node);
92 &node.elem
93 })
94 }
95}
96
97impl<'a, T> Iterator for IterMut<'a, T> {
98 type Item = &'a mut T;
99
100 fn next(&mut self) -> Option<Self::Item> {
101 self.next.take().map(|node| {
102 self.next = node.next.as_mut().map(|node| &mut **node);
103 &mut node.elem
104 })
105 }
106}
107
108
109#[cfg(test)]
110mod test {
111 use super::List;
112
113 #[test]
114 fn basics() {
115 let mut list = List::new();
116
117 assert_eq!(list.pop(), None);
119
120 list.push(1);
122 list.push(2);
123 list.push(3);
124
125 assert_eq!(list.pop(), Some(3));
127 assert_eq!(list.pop(), Some(2));
128
129 list.push(4);
131 list.push(5);
132
133 assert_eq!(list.pop(), Some(5));
135 assert_eq!(list.pop(), Some(4));
136
137 assert_eq!(list.pop(), Some(1));
139 assert_eq!(list.pop(), None);
140 }
141
142 #[test]
143 fn peek() {
144 let mut list = List::new();
145 assert_eq!(list.peek(), None);
146 assert_eq!(list.peek_mut(), None);
147 list.push(1); list.push(2); list.push(3);
148
149 assert_eq!(list.peek(), Some(&3));
150 assert_eq!(list.peek_mut(), Some(&mut 3));
151 }
152
153 #[test]
154 fn into_iter() {
155 let mut list = List::new();
156 list.push(1); list.push(2); list.push(3);
157
158 let mut iter = list.into_iter();
159 assert_eq!(iter.next(), Some(3));
160 assert_eq!(iter.next(), Some(2));
161 assert_eq!(iter.next(), Some(1));
162 }
163
164 #[test]
165 fn iter() {
166 let mut list = List::new();
167 list.push(1); list.push(2); list.push(3);
168
169 let mut iter = list.iter();
170 assert_eq!(iter.next(), Some(&3));
171 assert_eq!(iter.next(), Some(&2));
172 assert_eq!(iter.next(), Some(&1));
173 }
174
175 #[test]
176 fn iter_mut() {
177 let mut list = List::new();
178 list.push(1); list.push(2); list.push(3);
179
180 let mut iter = list.iter_mut();
181 assert_eq!(iter.next(), Some(&mut 3));
182 assert_eq!(iter.next(), Some(&mut 2));
183 assert_eq!(iter.next(), Some(&mut 1));
184 }
185}