indexmap/
util.rs

1use core::ops::{Bound, Range, RangeBounds};
2
3pub(crate) fn third<A, B, C>(t: (A, B, C)) -> C {
4    t.2
5}
6
7#[track_caller]
8pub(crate) fn simplify_range<R>(range: R, len: usize) -> Range<usize>
9where
10    R: RangeBounds<usize>,
11{
12    let start = match range.start_bound() {
13        Bound::Unbounded => 0,
14        Bound::Included(&i) if i <= len => i,
15        Bound::Excluded(&i) if i < len => i + 1,
16        Bound::Included(i) | Bound::Excluded(i) => {
17            panic!("range start index {i} out of range for slice of length {len}")
18        }
19    };
20    let end = match range.end_bound() {
21        Bound::Unbounded => len,
22        Bound::Excluded(&i) if i <= len => i,
23        Bound::Included(&i) if i < len => i + 1,
24        Bound::Included(i) | Bound::Excluded(i) => {
25            panic!("range end index {i} out of range for slice of length {len}")
26        }
27    };
28    if start > end {
29        panic!(
30            "range start index {:?} should be <= range end index {:?}",
31            range.start_bound(),
32            range.end_bound()
33        );
34    }
35    start..end
36}
37
38pub(crate) fn try_simplify_range<R>(range: R, len: usize) -> Option<Range<usize>>
39where
40    R: RangeBounds<usize>,
41{
42    let start = match range.start_bound() {
43        Bound::Unbounded => 0,
44        Bound::Included(&i) if i <= len => i,
45        Bound::Excluded(&i) if i < len => i + 1,
46        _ => return None,
47    };
48    let end = match range.end_bound() {
49        Bound::Unbounded => len,
50        Bound::Excluded(&i) if i <= len => i,
51        Bound::Included(&i) if i < len => i + 1,
52        _ => return None,
53    };
54    if start > end {
55        return None;
56    }
57    Some(start..end)
58}