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
use crate::{linked_list_slice::LinkedListSlice, linked_list_view::LinkedListView, LinkedList};
use std::ops::Deref;

// view
impl<'a, T: PartialEq> PartialEq for LinkedListView<'a, T> {
    fn eq(&self, other: &Self) -> bool {
        match (self.len(), other.len()) {
            (0, 0) => true,
            (a, b) if a != b => false,
            _ => {
                std::ptr::eq(
                    self.front_node().expect("issome"),
                    other.front_node().expect("issome"),
                ) && std::ptr::eq(
                    self.back_node().expect("issome"),
                    other.back_node().expect("issome"),
                ) || self.iter().zip(other.iter()).all(|(x, y)| x == y)
            }
        }
    }
}

impl<'s, 'a, T: PartialEq> PartialEq<LinkedListSlice<'s, 'a, T>> for LinkedListView<'a, T> {
    fn eq(&self, other: &LinkedListSlice<'s, 'a, T>) -> bool {
        let other = other.deref();
        self.eq(other.deref())
    }
}
impl<'a, T: PartialEq> PartialEq<LinkedList<'a, T>> for LinkedListView<'a, T> {
    fn eq(&self, other: &LinkedList<'a, T>) -> bool {
        let other = other.deref();
        self.eq(other.deref())
    }
}

impl<'a, T: PartialEq> PartialEq<[T]> for LinkedListView<'a, T> {
    fn eq(&self, other: &[T]) -> bool {
        other.eq(self)
    }
}

// asref(slice)
impl<'a, T: PartialEq, S: AsRef<[T]>> PartialEq<S> for LinkedListView<'a, T> {
    fn eq(&self, other: &S) -> bool {
        let other = other.as_ref();
        self.len() == other.len() && self.iter().zip(other.iter()).all(|(x, y)| x == y)
    }
}

impl<'s, 'a, T: PartialEq, S: AsRef<[T]>> PartialEq<S> for LinkedListSlice<'s, 'a, T> {
    fn eq(&self, other: &S) -> bool {
        self.deref().eq(other)
    }
}

impl<'a, T: PartialEq, S: AsRef<[T]>> PartialEq<S> for LinkedList<'a, T> {
    fn eq(&self, other: &S) -> bool {
        self.deref().eq(other)
    }
}

// slice
impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for [T] {
    fn eq(&self, other: &LinkedListView<'a, T>) -> bool {
        let slice = self;
        other.len() == slice.len() && other.iter().zip(slice.iter()).all(|(x, y)| x == y)
    }
}
impl<'s, 'a, T: PartialEq> PartialEq<LinkedListSlice<'s, 'a, T>> for [T] {
    fn eq(&self, other: &LinkedListSlice<'s, 'a, T>) -> bool {
        self.eq(other.deref())
    }
}
impl<'a, T: PartialEq> PartialEq<LinkedList<'a, T>> for [T] {
    fn eq(&self, other: &LinkedList<'a, T>) -> bool {
        self.eq(other.deref())
    }
}

// listslice
impl<'s, 'a, T: PartialEq> PartialEq for LinkedListSlice<'s, 'a, T> {
    fn eq(&self, other: &LinkedListSlice<'s, 'a, T>) -> bool {
        self.deref().eq(other.deref())
    }
}
impl<'s, 'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for LinkedListSlice<'s, 'a, T> {
    fn eq(&self, other: &LinkedListView<'a, T>) -> bool {
        other.eq(self.deref())
    }
}
impl<'s, 'a, T: PartialEq> PartialEq<LinkedList<'a, T>> for LinkedListSlice<'s, 'a, T> {
    fn eq(&self, other: &LinkedList<'a, T>) -> bool {
        other.deref().eq(self.deref())
    }
}

// list
impl<'a, T: PartialEq> PartialEq for LinkedList<'a, T> {
    fn eq(&self, other: &LinkedList<'a, T>) -> bool {
        self.deref().eq(other.deref())
    }
}
impl<'a, T: PartialEq> PartialEq<LinkedListView<'a, T>> for LinkedList<'a, T> {
    fn eq(&self, other: &LinkedListView<'a, T>) -> bool {
        other.eq(self.deref())
    }
}
impl<'s, 'a, T: PartialEq> PartialEq<LinkedListSlice<'s, 'a, T>> for LinkedList<'a, T> {
    fn eq(&self, other: &LinkedListSlice<'s, 'a, T>) -> bool {
        other.deref().eq(self.deref())
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use crate::LinkedList;

    #[test]
    fn eq_other() {
        let mut list1 = LinkedList::new();

        assert_eq!(list1, LinkedList::new());

        list1.push_back('a');
        list1.push_front('b');
        list1.push_front('c');
        list1.push_back('d');

        assert_eq!(&list1, &['c', 'b', 'a', 'd']);

        assert_eq!(list1, list1);
        assert_eq!(list1, list1.as_slice());
        assert_eq!(list1.as_slice(), list1);
        assert_eq!(list1.as_slice(), list1.as_slice());

        let (slice, _) = list1.split(4).unwrap();
        assert_eq!(slice, list1);
        assert_eq!(list1, slice);

        let (_, slice) = list1.split(0).unwrap();
        assert_eq!(slice, list1);
        assert_eq!(list1, slice);

        let (a, b) = list1.split(1).unwrap();
        assert_ne!(a, list1);
        assert_ne!(b, list1);
        assert_ne!(list1, a);
        assert_ne!(list1, b);

        let mut list2 = LinkedList::new();
        list2.push_back('a');
        list2.push_front('b');
        list2.push_front('c');
        assert_ne!(list1, list2);

        list2.push_back('d');
        assert_eq!(list1, list2);
    }

    #[test]
    fn eq_asref() {
        let mut list = LinkedList::new();

        list.push_back('a');
        list.push_front('b');
        list.push_front('c');
        list.push_back('d');

        assert_eq!(list, &['c', 'b', 'a', 'd']);
        assert_eq!(list.as_slice(), &['c', 'b', 'a', 'd']);

        let (a, b) = list.split(1).expect("within bounds");
        assert_eq!(a, &['c']);
        assert_eq!(b, &['b', 'a', 'd']);
    }
}