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
use std::rc::Rc;
#[derive(PartialEq)]
pub struct List<T> {
head: Option<Rc<Node<T>>>,
}
#[derive(PartialEq)]
struct Node<T> {
next: Option<Rc<Node<T>>>,
data: T,
}
#[macro_export]
macro_rules! list {
() => ($crate::list::List::nil());
($x:expr) => ($crate::list::List::cons($x, &list![]));
($x:expr, $($xs:expr),*) => ($crate::list::List::cons($x, &list![$($xs),*]));
}
impl<T> List<T> {
pub fn nil() -> Self {
List { head: None }
}
pub fn cons(data: T, next: &Self) -> Self {
let node = Node { data, next: next.head.clone() };
List { head: Some(Rc::new(node)) }
}
pub fn decons(&self) -> Option<(&T, Self)> {
self.head.as_ref().map(|node| (&node.data, List { head: node.next.clone() }))
}
pub fn head(&self) -> Option<&T> {
self.head.as_ref().map(|node| &node.data)
}
pub fn tail(&self) -> Option<Self> {
self.head.as_ref().map(|node| List { head: node.next.clone() })
}
pub fn is_empty(&self) -> bool {
self.head.is_none()
}
pub fn len(&self) -> usize {
self.iter().count()
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
std::iter::successors(self.head.as_ref(), |node| node.next.as_ref()).map(|node| &node.data)
}
}
impl<T> Drop for List<T> {
fn drop(&mut self) {
let mut next = self.head.take();
while let Some(node) = next {
if let Ok(mut node) = Rc::try_unwrap(node) {
next = node.next.take();
} else {
break;
}
}
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for List<T> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.debug_list().entries(self.iter()).finish()
}
}
#[cfg(test)]
mod tests {
use super::List;
#[test]
fn macro_list() {
assert_eq!(list![], List::<()>::nil());
assert_eq!(list![1], List::cons(1, &List::nil()));
assert_eq!(list![1, 2], List::cons(1, &List::cons(2, &List::nil())));
}
#[test]
fn decons() {
assert_eq!((list![] as List<()>).decons(), None);
assert_eq!(list![1].decons(), Some((&1, list![])));
assert_eq!(list![1, 2].decons(), Some((&1, list![2])));
}
#[test]
fn head() {
assert_eq!((list![] as List<()>).head(), None);
assert_eq!(list![1].head(), Some(&1));
assert_eq!(list![1, 2].head(), Some(&1));
}
#[test]
fn tail() {
assert_eq!((list![] as List<()>).tail(), None);
assert_eq!(list![1].tail(), Some(list![]));
assert_eq!(list![1, 2].tail(), Some(list![2]));
}
#[test]
fn is_empty() {
assert!((list![] as List<()>).is_empty());
assert!(!list![1].is_empty());
assert!(!list![1, 2].is_empty());
}
#[test]
fn len() {
assert_eq!((list![] as List<()>).len(), 0);
assert_eq!(list![1].len(), 1);
assert_eq!(list![1, 2].len(), 2);
}
#[test]
fn fmt() {
assert_eq!(format!("{:?}", list![] as List<()>), "[]");
assert_eq!(format!("{:?}", list![1]), "[1]");
assert_eq!(format!("{:?}", list![1, 2]), "[1, 2]");
}
#[test]
fn iter() {
let h = |xs: List<_>| xs.iter().cloned().collect::<Vec<_>>();
assert_eq!(h(list![]), []);
assert_eq!(h(list![1]), [1]);
assert_eq!(h(list![1, 2]), [1, 2]);
}
}