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
use crate::{
    list::List,
    variants::{ends::ListEnds, list_variant::ListVariant},
};

impl<'a, V, T> Clone for List<'a, V, T>
where
    V: ListVariant<'a, T>,
    V::Ends: ListEnds<'a, V, T>,
    T: Clone,
    Self: FromIterator<T>,
{
    fn clone(&self) -> Self {
        Self::from_iter(self.iter().cloned())
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn clone_empty() {
        let list = DoublyLinkedList::<char>::new();
        let clone = list.clone();
        assert!(clone.is_empty());

        let list = SinglyLinkedList::<char>::new();
        let clone = list.clone();
        assert!(clone.is_empty());
    }

    #[test]
    fn clone_single() {
        let mut list = DoublyLinkedList::<char>::new();
        list.push_back('a');
        let clone = list.clone();

        assert_eq!(1, clone.len());
        assert_eq!(Some(&'a'), clone.front());
        assert_eq!(Some(&'a'), clone.back());

        let mut list = SinglyLinkedList::<char>::new();
        list.push_front('a');
        let clone = list.clone();

        assert_eq!(1, clone.len());
        assert_eq!(Some(&'a'), clone.front());
        assert_eq!(Some(&'a'), clone.back());
    }

    #[test]
    fn clone_multi() {
        let mut list = DoublyLinkedList::<char>::new();
        list.push_back('a');
        list.push_back('b');
        list.push_back('c');
        let clone = list.clone();

        assert_eq!(3, clone.len());
        assert_eq!(Some(&'a'), clone.front());
        assert_eq!(Some(&'c'), clone.back());

        let mut list = SinglyLinkedList::<char>::new();
        list.push_front('c');
        list.push_front('b');
        list.push_front('a');
        let clone = list.clone();

        assert_eq!(3, clone.len());
        assert_eq!(Some(&'a'), clone.front());
        assert_eq!(Some(&'c'), clone.back());
    }
}