iterable/impls/collections/
linked_list.rs1use std::collections::LinkedList;
2
3use crate::{Iterable, IterableSeq};
4
5impl<T> Iterable for LinkedList<T> {
6 type C = Self;
7 type CC<U> = LinkedList<U>;
8 type F = Self;
10 type CF<U> = LinkedList<U>;
11
12 fn add_one(mut self, a: Self::Item) -> Self::C {
13 self.push_back(a);
14 self
15 }
16}
17
18impl<'a, T> Iterable for &'a LinkedList<T> {
19 type C = LinkedList<&'a T>;
20 type CC<U> = LinkedList<U>;
21 type F = LinkedList<&'a T>;
23 type CF<U> = LinkedList<U>;
24}
25
26impl<T> IterableSeq for LinkedList<T> {}
27impl<'a, T> IterableSeq for &'a LinkedList<T> {}
28
29delegate_into_iterator!(LinkedList<T>, impl <T>);
30delegate_into_iterator!(&'a LinkedList<T>, impl <'a, T: 'a>);
31
32delegate_from_iterator!(LinkedList<T>, T, impl <T>);
33delegate_extend!(LinkedList<T>, T, impl <T>);
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 fn ll<T>(v: Vec<T>) -> LinkedList<T> {
40 v.into_iter().collect()
41 }
42
43 #[test]
44 fn test_c() {
45 let v = ll(vec![1, 2, 3]);
46 let res = v.filter(|i| i > &1);
47 assert_eq!(res, ll(vec![2, 3]));
48 }
49
50 #[test]
51 fn test_cc() {
52 let v = ll(vec![1, 2, 3]);
53 let res = v.map(|i| i.to_string());
54 assert_eq!(
55 res,
56 ll(vec!["1".to_string(), "2".to_string(), "3".to_string()])
57 );
58 }
59
60 #[test]
61 fn test_c_r() {
62 let v = ll(vec![1, 2, 3]);
63 let res = (&v).filter(|i| i > &&1);
64 assert_eq!(res, ll(vec![&2, &3]));
65 }
66
67 #[test]
68 fn test_cc_r() {
69 let v = ll(vec![1, 2, 3]);
70 let res = (&v).map(|i| i.to_string());
71 assert_eq!(
72 res,
73 ll(vec!["1".to_string(), "2".to_string(), "3".to_string()])
74 );
75 }
76}