| 1 | use std::sync::Arc; |
| 2 | |
| 3 | /// An immutable singly-linked list. |
| 4 | pub struct List<T> { |
| 5 | head: Option<Arc<Node<T>>>, |
| 6 | } |
| 7 | |
| 8 | impl<T: Clone> Clone for List<T> { |
| 9 | fn clone(&self) -> Self { |
| 10 | List { |
| 11 | head: self.head.clone(), |
| 12 | } |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl<T: std::fmt::Debug> std::fmt::Debug for List<T> { |
| 17 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 18 | f.debug_list().entries(self.iter()).finish() |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl<T: PartialEq> PartialEq for List<T> { |
| 23 | fn eq(&self, other: &Self) -> bool { |
| 24 | self.iter().eq(other.iter()) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl<T: Eq> Eq for List<T> {} |
| 29 | |
| 30 | impl<T: std::hash::Hash> std::hash::Hash for List<T> { |
| 31 | fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 32 | for item in self { |
| 33 | item.hash(state); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl<T> Drop for List<T> { |
| 39 | fn drop(&mut self) { |
| 40 | let mut current = self.head.take(); |
| 41 | while let Some(node) = current { |
| 42 | if let Ok(mut node) = Arc::try_unwrap(node) { |
| 43 | current = node.next.take(); |
| 44 | } else { |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl<T> List<T> { |
| 52 | pub(crate) fn new() -> Self { |
| 53 | Self { head: None } |
| 54 | } |
| 55 | /// Returns true if the list contains no elements. |
| 56 | #[must_use] |
| 57 | pub fn is_empty(&self) -> bool { |
| 58 | self.head.is_none() |
| 59 | } |
| 60 | /// Creates a new list with the given value at the front, sharing the rest of the nodes. |
| 61 | #[must_use] |
| 62 | pub fn push_front(&self, value: Arc<T>) -> Self { |
| 63 | List { |
| 64 | head: Some(Arc::new(Node { |
| 65 | value, |
| 66 | next: self.head.clone(), |
| 67 | })), |
| 68 | } |
| 69 | } |
| 70 | /// Returns an iterator over references to the list elements. |
| 71 | #[must_use] |
| 72 | pub fn iter(&self) -> Iter<'_, T> { |
| 73 | Iter { |
| 74 | current: self.head.as_ref(), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[derive(Debug)] |
| 80 | pub(crate) struct Node<T> { |
| 81 | value: Arc<T>, |
| 82 | next: Option<Arc<Node<T>>>, |
| 83 | } |
| 84 | |
| 85 | /// Iterator over references to elements in a `List`. |
| 86 | #[derive(Debug)] |
| 87 | pub struct Iter<'a, T> { |
| 88 | current: Option<&'a Arc<Node<T>>>, |
| 89 | } |
| 90 | |
| 91 | impl<'a, T> Iterator for Iter<'a, T> { |
| 92 | type Item = &'a T; |
| 93 | |
| 94 | fn next(&mut self) -> Option<Self::Item> { |
| 95 | self.current.map(|current| { |
| 96 | let value = ¤t.value; |
| 97 | self.current = current.next.as_ref(); |
| 98 | &**value |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl<'a, T> IntoIterator for &'a List<T> { |
| 104 | type IntoIter = Iter<'a, T>; |
| 105 | type Item = &'a T; |
| 106 | fn into_iter(self) -> Self::IntoIter { |
| 107 | self.iter() |
| 108 | } |
| 109 | } |