dogma/traits/
collection.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::prelude::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, Vec, VecDeque};
4
5#[cfg(feature = "std")]
6use crate::prelude::{Hash, HashMap, HashSet};
7
8/// A trait for collections of items.
9pub trait Collection {
10    type Item;
11
12    /// Returns the number of items in the collection.
13    fn len(&self) -> usize;
14
15    /// Checks whether the collection is empty.
16    fn is_empty(&self) -> bool {
17        self.len() == 0
18    }
19
20    /// Checks whether the collection is nonempty.
21    fn is_nonempty(&self) -> bool {
22        !self.is_empty()
23    }
24}
25
26// Implementation for fixed-size arrays
27impl<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
39// Implementation for slices
40impl<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
52// Implementation for `Vec<T>`
53impl<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
65// Implementation for `VecDeque<T>`
66impl<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
78// Implementation for `LinkedList<T>`
79impl<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
91// Implementation for `BinaryHeap<T>`
92impl<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
104// Implementation for `BTreeSet<T>`
105impl<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
117// Implementation for `BTreeMap<K, V>`
118impl<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// Implementation for `HashSet<T>`
131#[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// Implementation for `HashMap<K, V>`
145#[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}