|  | 
| 1 | 1 | //! Indexing implementations for `[T]`. | 
| 2 | 2 | 
 | 
| 3 |  | -use crate::ops::{self, Bound, Range, RangeBounds}; | 
|  | 3 | +use crate::ops; | 
| 4 | 4 | use crate::ptr; | 
| 5 | 5 | 
 | 
| 6 | 6 | #[stable(feature = "rust1", since = "1.0.0")] | 
| @@ -37,104 +37,31 @@ fn slice_start_index_len_fail(index: usize, len: usize) -> ! { | 
| 37 | 37 | #[inline(never)] | 
| 38 | 38 | #[cold] | 
| 39 | 39 | #[track_caller] | 
| 40 |  | -pub(super) fn slice_end_index_len_fail(index: usize, len: usize) -> ! { | 
|  | 40 | +pub(crate) fn slice_end_index_len_fail(index: usize, len: usize) -> ! { | 
| 41 | 41 |     panic!("range end index {} out of range for slice of length {}", index, len); | 
| 42 | 42 | } | 
| 43 | 43 | 
 | 
| 44 | 44 | #[inline(never)] | 
| 45 | 45 | #[cold] | 
| 46 | 46 | #[track_caller] | 
| 47 |  | -pub(super) fn slice_index_order_fail(index: usize, end: usize) -> ! { | 
|  | 47 | +pub(crate) fn slice_index_order_fail(index: usize, end: usize) -> ! { | 
| 48 | 48 |     panic!("slice index starts at {} but ends at {}", index, end); | 
| 49 | 49 | } | 
| 50 | 50 | 
 | 
| 51 | 51 | #[inline(never)] | 
| 52 | 52 | #[cold] | 
| 53 | 53 | #[track_caller] | 
| 54 |  | -pub(super) fn slice_start_index_overflow_fail() -> ! { | 
|  | 54 | +pub(crate) fn slice_start_index_overflow_fail() -> ! { | 
| 55 | 55 |     panic!("attempted to index slice from after maximum usize"); | 
| 56 | 56 | } | 
| 57 | 57 | 
 | 
| 58 | 58 | #[inline(never)] | 
| 59 | 59 | #[cold] | 
| 60 | 60 | #[track_caller] | 
| 61 |  | -pub(super) fn slice_end_index_overflow_fail() -> ! { | 
|  | 61 | +pub(crate) fn slice_end_index_overflow_fail() -> ! { | 
| 62 | 62 |     panic!("attempted to index slice up to maximum usize"); | 
| 63 | 63 | } | 
| 64 | 64 | 
 | 
| 65 |  | -/// Performs bounds-checking of the given range. | 
| 66 |  | -/// The returned [`Range`] is safe to pass to [`get_unchecked`] and [`get_unchecked_mut`] | 
| 67 |  | -/// for slices of the given length. | 
| 68 |  | -/// | 
| 69 |  | -/// [`get_unchecked`]: ../../std/primitive.slice.html#method.get_unchecked | 
| 70 |  | -/// [`get_unchecked_mut`]: ../../std/primitive.slice.html#method.get_unchecked_mut | 
| 71 |  | -/// | 
| 72 |  | -/// # Panics | 
| 73 |  | -/// | 
| 74 |  | -/// Panics if the range is out of bounds. | 
| 75 |  | -/// | 
| 76 |  | -/// # Examples | 
| 77 |  | -/// | 
| 78 |  | -/// ``` | 
| 79 |  | -/// #![feature(slice_check_range)] | 
| 80 |  | -/// use std::slice; | 
| 81 |  | -/// | 
| 82 |  | -/// let v = [10, 40, 30]; | 
| 83 |  | -/// assert_eq!(1..2, slice::check_range(v.len(), 1..2)); | 
| 84 |  | -/// assert_eq!(0..2, slice::check_range(v.len(), ..2)); | 
| 85 |  | -/// assert_eq!(1..3, slice::check_range(v.len(), 1..)); | 
| 86 |  | -/// ``` | 
| 87 |  | -/// | 
| 88 |  | -/// Panics when [`Index::index`] would panic: | 
| 89 |  | -/// | 
| 90 |  | -/// ```should_panic | 
| 91 |  | -/// #![feature(slice_check_range)] | 
| 92 |  | -/// | 
| 93 |  | -/// std::slice::check_range(3, 2..1); | 
| 94 |  | -/// ``` | 
| 95 |  | -/// | 
| 96 |  | -/// ```should_panic | 
| 97 |  | -/// #![feature(slice_check_range)] | 
| 98 |  | -/// | 
| 99 |  | -/// std::slice::check_range(3, 1..4); | 
| 100 |  | -/// ``` | 
| 101 |  | -/// | 
| 102 |  | -/// ```should_panic | 
| 103 |  | -/// #![feature(slice_check_range)] | 
| 104 |  | -/// | 
| 105 |  | -/// std::slice::check_range(3, 1..=usize::MAX); | 
| 106 |  | -/// ``` | 
| 107 |  | -/// | 
| 108 |  | -/// [`Index::index`]: ops::Index::index | 
| 109 |  | -#[track_caller] | 
| 110 |  | -#[unstable(feature = "slice_check_range", issue = "76393")] | 
| 111 |  | -pub fn check_range<R: RangeBounds<usize>>(len: usize, range: R) -> Range<usize> { | 
| 112 |  | -    let start = match range.start_bound() { | 
| 113 |  | -        Bound::Included(&start) => start, | 
| 114 |  | -        Bound::Excluded(start) => { | 
| 115 |  | -            start.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail()) | 
| 116 |  | -        } | 
| 117 |  | -        Bound::Unbounded => 0, | 
| 118 |  | -    }; | 
| 119 |  | - | 
| 120 |  | -    let end = match range.end_bound() { | 
| 121 |  | -        Bound::Included(end) => { | 
| 122 |  | -            end.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail()) | 
| 123 |  | -        } | 
| 124 |  | -        Bound::Excluded(&end) => end, | 
| 125 |  | -        Bound::Unbounded => len, | 
| 126 |  | -    }; | 
| 127 |  | - | 
| 128 |  | -    if start > end { | 
| 129 |  | -        slice_index_order_fail(start, end); | 
| 130 |  | -    } | 
| 131 |  | -    if end > len { | 
| 132 |  | -        slice_end_index_len_fail(end, len); | 
| 133 |  | -    } | 
| 134 |  | - | 
| 135 |  | -    Range { start, end } | 
| 136 |  | -} | 
| 137 |  | - | 
| 138 | 65 | mod private_slice_index { | 
| 139 | 66 |     use super::ops; | 
| 140 | 67 |     #[stable(feature = "slice_get_slice", since = "1.28.0")] | 
|  | 
0 commit comments