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