dogma/traits/
collection.rs1use crate::prelude::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, Vec, VecDeque};
4
5#[cfg(feature = "std")]
6use crate::prelude::{Hash, HashMap, HashSet};
7
8pub trait Collection {
10 type Item;
11
12 fn len(&self) -> usize;
14
15 fn is_empty(&self) -> bool {
17 self.len() == 0
18 }
19
20 fn is_nonempty(&self) -> bool {
22 !self.is_empty()
23 }
24}
25
26impl<T, const N: usize> Collection for [T; N] {
28 type Item = T;
29
30 fn len(&self) -> usize {
31 N
32 }
33
34 fn is_empty(&self) -> bool {
35 N == 0
36 }
37}
38
39impl<T> Collection for [T] {
41 type Item = T;
42
43 fn len(&self) -> usize {
44 self.len()
45 }
46
47 fn is_empty(&self) -> bool {
48 self.is_empty()
49 }
50}
51
52impl<T> Collection for Vec<T> {
54 type Item = T;
55
56 fn len(&self) -> usize {
57 self.len()
58 }
59
60 fn is_empty(&self) -> bool {
61 self.is_empty()
62 }
63}
64
65impl<T> Collection for VecDeque<T> {
67 type Item = T;
68
69 fn len(&self) -> usize {
70 self.len()
71 }
72
73 fn is_empty(&self) -> bool {
74 self.is_empty()
75 }
76}
77
78impl<T> Collection for LinkedList<T> {
80 type Item = T;
81
82 fn len(&self) -> usize {
83 self.len()
84 }
85
86 fn is_empty(&self) -> bool {
87 self.is_empty()
88 }
89}
90
91impl<T: Ord> Collection for BinaryHeap<T> {
93 type Item = T;
94
95 fn len(&self) -> usize {
96 self.len()
97 }
98
99 fn is_empty(&self) -> bool {
100 self.is_empty()
101 }
102}
103
104impl<T: Ord> Collection for BTreeSet<T> {
106 type Item = T;
107
108 fn len(&self) -> usize {
109 self.len()
110 }
111
112 fn is_empty(&self) -> bool {
113 self.is_empty()
114 }
115}
116
117impl<K: Ord, V> Collection for BTreeMap<K, V> {
119 type Item = (K, V);
120
121 fn len(&self) -> usize {
122 self.len()
123 }
124
125 fn is_empty(&self) -> bool {
126 self.is_empty()
127 }
128}
129
130#[cfg(feature = "std")]
132impl<T: Eq + Hash> Collection for HashSet<T> {
133 type Item = T;
134
135 fn len(&self) -> usize {
136 self.len()
137 }
138
139 fn is_empty(&self) -> bool {
140 self.is_empty()
141 }
142}
143
144#[cfg(feature = "std")]
146impl<K: Eq + Hash, V> Collection for HashMap<K, V> {
147 type Item = (K, V);
148
149 fn len(&self) -> usize {
150 self.len()
151 }
152
153 fn is_empty(&self) -> bool {
154 self.is_empty()
155 }
156}