/rust/registry/src/index.crates.io-6f17d22bba15001f/indexmap-2.10.0/src/util.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use core::ops::{Bound, Range, RangeBounds}; |
2 | | |
3 | 0 | pub(crate) fn third<A, B, C>(t: (A, B, C)) -> C { |
4 | 0 | t.2 |
5 | 0 | } Unexecuted instantiation: indexmap::util::third::<usize, serde_yaml::value::Value, serde_yaml::value::Value> Unexecuted instantiation: indexmap::util::third::<_, _, _> |
6 | | |
7 | | #[track_caller] |
8 | 0 | pub(crate) fn simplify_range<R>(range: R, len: usize) -> Range<usize> |
9 | 0 | where |
10 | 0 | R: RangeBounds<usize>, |
11 | 0 | { |
12 | 0 | let start = match range.start_bound() { |
13 | 0 | Bound::Unbounded => 0, |
14 | 0 | Bound::Included(&i) if i <= len => i, |
15 | 0 | Bound::Excluded(&i) if i < len => i + 1, |
16 | 0 | Bound::Included(i) | Bound::Excluded(i) => { |
17 | 0 | panic!("range start index {i} out of range for slice of length {len}") |
18 | | } |
19 | | }; |
20 | 0 | let end = match range.end_bound() { |
21 | 0 | Bound::Unbounded => len, |
22 | 0 | Bound::Excluded(&i) if i <= len => i, |
23 | 0 | Bound::Included(&i) if i < len => i + 1, |
24 | 0 | Bound::Included(i) | Bound::Excluded(i) => { |
25 | 0 | panic!("range end index {i} out of range for slice of length {len}") |
26 | | } |
27 | | }; |
28 | 0 | if start > end { |
29 | 0 | panic!( |
30 | 0 | "range start index {:?} should be <= range end index {:?}", |
31 | 0 | range.start_bound(), |
32 | 0 | range.end_bound() |
33 | 0 | ); |
34 | 0 | } |
35 | 0 | start..end |
36 | 0 | } |
37 | | |
38 | 0 | pub(crate) fn try_simplify_range<R>(range: R, len: usize) -> Option<Range<usize>> |
39 | 0 | where |
40 | 0 | R: RangeBounds<usize>, |
41 | 0 | { |
42 | 0 | let start = match range.start_bound() { |
43 | 0 | Bound::Unbounded => 0, |
44 | 0 | Bound::Included(&i) if i <= len => i, |
45 | 0 | Bound::Excluded(&i) if i < len => i + 1, |
46 | 0 | _ => return None, |
47 | | }; |
48 | 0 | let end = match range.end_bound() { |
49 | 0 | Bound::Unbounded => len, |
50 | 0 | Bound::Excluded(&i) if i <= len => i, |
51 | 0 | Bound::Included(&i) if i < len => i + 1, |
52 | 0 | _ => return None, |
53 | | }; |
54 | 0 | if start > end { |
55 | 0 | return None; |
56 | 0 | } |
57 | 0 | Some(start..end) |
58 | 0 | } |
59 | | |
60 | | // Generic slice equality -- copied from the standard library but adding a custom comparator, |
61 | | // allowing for our `Bucket` wrapper on either or both sides. |
62 | 0 | pub(crate) fn slice_eq<T, U>(left: &[T], right: &[U], eq: impl Fn(&T, &U) -> bool) -> bool { |
63 | 0 | if left.len() != right.len() { |
64 | 0 | return false; |
65 | 0 | } |
66 | | |
67 | | // Implemented as explicit indexing rather |
68 | | // than zipped iterators for performance reasons. |
69 | | // See PR https://github.com/rust-lang/rust/pull/116846 |
70 | 0 | for i in 0..left.len() { |
71 | | // bound checks are optimized away |
72 | 0 | if !eq(&left[i], &right[i]) { |
73 | 0 | return false; |
74 | 0 | } |
75 | | } |
76 | | |
77 | 0 | true |
78 | 0 | } |