Skip to content

Commit c98fbb4

Browse files
committed
Support arrays of any size in collection matchers.
This was possible thanks to the [stabilisation of minimal const generics support](rust-lang/rust#79135) in Rust 1.51.
1 parent d74d3be commit c98fbb4

File tree

1 file changed

+12
-69
lines changed

1 file changed

+12
-69
lines changed

src/matchers/collection.rs

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use std::marker::PhantomData;
44

55
/// Matches if `actual` contains `element`.
66
///
7-
/// Supports [arrays] of up to 256 elements, [`Vec`]s, [`VecDeque`]s, [`LinkedList`]s, [`HashSet`]s
8-
/// and [`BTreeSet`]s.
7+
/// Supports [arrays] [`Vec`]s, [`VecDeque`]s, [`LinkedList`]s, [`HashSet`]s and [`BTreeSet`]s.
98
///
109
/// [array]: https://doc.rust-lang.org/std/primitive.array.html
1110
/// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
@@ -44,8 +43,7 @@ impl<T: std::fmt::Debug, V: Collection<T>> Matcher<V> for ContainMatcher<T> {
4443

4544
/// Matches if `actual` is empty.
4645
///
47-
/// Supports [arrays] of up to 256 elements, [`Vec`]s, [`VecDeque`]s, [`LinkedList`]s, [`HashSet`]s
48-
/// and [`BTreeSet`]s.
46+
/// Supports [arrays], [`Vec`]s, [`VecDeque`]s, [`LinkedList`]s, [`HashSet`]s and [`BTreeSet`]s.
4947
///
5048
/// [array]: https://doc.rust-lang.org/std/primitive.array.html
5149
/// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
@@ -88,38 +86,15 @@ pub trait Collection<T> {
8886
fn empty(&self) -> bool;
8987
}
9088

91-
macro_rules! array {
92-
($($N:expr),+) => {
93-
$(
94-
#[doc(hidden)]
95-
impl<T: std::cmp::PartialEq> Collection<T> for [T; $N] {
96-
fn contains_element(&self, element: &T) -> bool {
97-
self.contains(element)
98-
}
99-
100-
fn empty(&self) -> bool {
101-
self.is_empty()
102-
}
103-
}
104-
)+
89+
impl<T: std::cmp::PartialEq, const N: usize> Collection<T> for [T; N] {
90+
fn contains_element(&self, element: &T) -> bool {
91+
self.contains(element)
10592
}
106-
}
10793

108-
array!(
109-
0, 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,
110-
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
111-
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
112-
74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
113-
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
114-
117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
115-
136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
116-
155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173,
117-
174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192,
118-
193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211,
119-
212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230,
120-
231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
121-
250, 251, 252, 253, 254, 255, 256
122-
);
94+
fn empty(&self) -> bool {
95+
self.is_empty()
96+
}
97+
}
12398

12499
impl<T: std::cmp::PartialEq> Collection<T> for std::vec::Vec<T> {
125100
fn contains_element(&self, element: &T) -> bool {
@@ -211,41 +186,9 @@ mod tests {
211186
}
212187

213188
#[test]
214-
fn arrays_with_up_to_256_elements_are_collections() {
215-
assert!([
216-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
217-
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
218-
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
219-
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
220-
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
221-
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
222-
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
223-
143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
224-
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
225-
177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
226-
194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
227-
211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
228-
228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244,
229-
245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
230-
]
231-
.contains_element(&255));
232-
assert!(![
233-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
234-
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
235-
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
236-
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
237-
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
238-
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
239-
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
240-
143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
241-
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
242-
177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
243-
194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
244-
211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
245-
228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244,
246-
245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
247-
]
248-
.empty());
189+
fn arrays_are_collections() {
190+
assert!([1, 2, 3].contains_element(&2));
191+
assert!(![1, 2, 3].empty());
249192
}
250193

251194
#[test]

0 commit comments

Comments
 (0)