Debian Codegraph @

src/set/slice.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use super::OrderSet;
2 use core::ops::{self, Bound, Index};
3 use indexmap::set::Slice;
4
5 // We can't have `impl<I: RangeBounds<usize>> Index<I>` because that conflicts with `Index<usize>`.
6 // Instead, we repeat the implementations for all the core range types.
7 macro_rules! impl_index {
8 ($($range:ty),*) => {$(
9 impl<T, S> Index<$range> for OrderSet<T, S> {
10 type Output = Slice<T>;
11
12 fn index(&self, range: $range) -> &Self::Output {
13 &self.inner[range]
14 }
15 }
16 )*}
17 }
18 impl_index!(
19 ops::Range<usize>,
20 ops::RangeFrom<usize>,
21 ops::RangeFull,
22 ops::RangeInclusive<usize>,
23 ops::RangeTo<usize>,
24 ops::RangeToInclusive<usize>,
25 (Bound<usize>, Bound<usize>)
26 );
27
•28 #[cfg(all(test, feature = "std"))]
29 mod tests {
30 use super::*;
31 use alloc::vec::Vec;
32
33 #[test]
34 fn slice_index() {
35 fn check(vec_slice: &[i32], set_slice: &Slice<i32>, sub_slice: &Slice<i32>) {
36 assert_eq!(set_slice as *const _, sub_slice as *const _);
37 itertools::assert_equal(vec_slice, set_slice);
38 }
39
40 let vec: Vec<i32> = (0..10).map(|i| i * i).collect();
41 let set: OrderSet<i32> = vec.iter().cloned().collect();
42 let slice = set.as_slice();
43
44 // RangeFull
45 check(&vec[..], &set[..], &slice[..]);
46
47 for i in 0usize..10 {
48 // Index
49 assert_eq!(vec[i], set[i]);
50 assert_eq!(vec[i], slice[i]);
51
52 // RangeFrom
53 check(&vec[i..], &set[i..], &slice[i..]);
54
55 // RangeTo
56 check(&vec[..i], &set[..i], &slice[..i]);
57
58 // RangeToInclusive
59 check(&vec[..=i], &set[..=i], &slice[..=i]);
60
61 // (Bound<usize>, Bound<usize>)
62 let bounds = (Bound::Excluded(i), Bound::Unbounded);
63 check(&vec[i + 1..], &set[bounds], &slice[bounds]);
64
65 for j in i..=10 {
66 // Range
67 check(&vec[i..j], &set[i..j], &slice[i..j]);
68 }
69
70 for j in i..10 {
71 // RangeInclusive
72 check(&vec[i..=j], &set[i..=j], &slice[i..=j]);
73 }
74 }
75 }
76 }