|
| 1 | +use std::ops; |
| 2 | + |
| 3 | +pub trait SliceIndex<T: ?Sized> { |
| 4 | + /// The output type returned by methods. |
| 5 | + type Output: ?Sized; |
| 6 | + |
| 7 | + /// Returns a shared reference to the output at this location, without |
| 8 | + /// performing any bounds checking. |
| 9 | + unsafe fn get_unchecked(self, slice: &T) -> &Self::Output; |
| 10 | + |
| 11 | + /// Returns a shared reference to the output at this location, panicking |
| 12 | + /// if out of bounds. |
| 13 | + fn index(self, slice: &T) -> &Self::Output; |
| 14 | +} |
| 15 | + |
| 16 | +impl<T> SliceIndex<[T]> for usize { |
| 17 | + type Output = T; |
| 18 | + |
| 19 | + #[inline] |
| 20 | + unsafe fn get_unchecked(self, slice: &[T]) -> &T { |
| 21 | + &*slice.as_ptr().offset(self as isize) |
| 22 | + } |
| 23 | + |
| 24 | + #[inline] |
| 25 | + fn index(self, slice: &[T]) -> &T { |
| 26 | + // NB: use intrinsic indexing |
| 27 | + &(*slice)[self] |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[inline(never)] |
| 32 | +#[cold] |
| 33 | +fn slice_index_len_fail(index: usize, len: usize) -> ! { |
| 34 | + panic!("index {} out of range for slice of length {}", index, len); |
| 35 | +} |
| 36 | + |
| 37 | +#[inline(never)] |
| 38 | +#[cold] |
| 39 | +fn slice_index_order_fail(index: usize, end: usize) -> ! { |
| 40 | + panic!("slice index starts at {} but ends at {}", index, end); |
| 41 | +} |
| 42 | + |
| 43 | +impl<T> SliceIndex<[T]> for ops::Range<usize> { |
| 44 | + type Output = [T]; |
| 45 | + |
| 46 | + #[inline] |
| 47 | + unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { |
| 48 | + use std::slice::from_raw_parts; |
| 49 | + from_raw_parts(slice.as_ptr().offset(self.start as isize), |
| 50 | + self.end - self.start) |
| 51 | + } |
| 52 | + |
| 53 | + #[inline] |
| 54 | + fn index(self, slice: &[T]) -> &[T] { |
| 55 | + if self.start > self.end { |
| 56 | + slice_index_order_fail(self.start, self.end); |
| 57 | + } else if self.end > slice.len() { |
| 58 | + slice_index_len_fail(self.end, slice.len()); |
| 59 | + } |
| 60 | + unsafe { self.get_unchecked(slice) } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<T> SliceIndex<[T]> for ops::RangeTo<usize> { |
| 65 | + type Output = [T]; |
| 66 | + |
| 67 | + #[inline] |
| 68 | + unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { |
| 69 | + (0..self.end).get_unchecked(slice) |
| 70 | + } |
| 71 | + |
| 72 | + #[inline] |
| 73 | + fn index(self, slice: &[T]) -> &[T] { |
| 74 | + (0..self.end).index(slice) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> { |
| 79 | + type Output = [T]; |
| 80 | + |
| 81 | + #[inline] |
| 82 | + unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { |
| 83 | + (self.start..slice.len()).get_unchecked(slice) |
| 84 | + } |
| 85 | + |
| 86 | + #[inline] |
| 87 | + fn index(self, slice: &[T]) -> &[T] { |
| 88 | + (self.start..slice.len()).index(slice) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<T> SliceIndex<[T]> for ops::RangeFull { |
| 93 | + type Output = [T]; |
| 94 | + |
| 95 | + #[inline] |
| 96 | + unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { |
| 97 | + slice |
| 98 | + } |
| 99 | + |
| 100 | + #[inline] |
| 101 | + fn index(self, slice: &[T]) -> &[T] { |
| 102 | + slice |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// nightly only: |
| 107 | +// |
| 108 | +// impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> { |
| 109 | +// type Output = [T]; |
| 110 | +// |
| 111 | +// #[inline] |
| 112 | +// unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { |
| 113 | +// match self { |
| 114 | +// ops::RangeInclusive::Empty { .. } => &[], |
| 115 | +// ops::RangeInclusive::NonEmpty { start, end } => (start..end + 1).get_unchecked(slice), |
| 116 | +// } |
| 117 | +// } |
| 118 | +// |
| 119 | +// #[inline] |
| 120 | +// fn index(self, slice: &[T]) -> &[T] { |
| 121 | +// match self { |
| 122 | +// ops::RangeInclusive::Empty { .. } => &[], |
| 123 | +// ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => { |
| 124 | +// panic!("attempted to index slice up to maximum usize"); |
| 125 | +// } |
| 126 | +// ops::RangeInclusive::NonEmpty { start, end } => (start..end + 1).index(slice), |
| 127 | +// } |
| 128 | +// } |
| 129 | +// } |
| 130 | +// |
| 131 | +// impl<T> SliceIndex<[T]> for ops::RangeToInclusive<usize> { |
| 132 | +// type Output = [T]; |
| 133 | +// |
| 134 | +// #[inline] |
| 135 | +// unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { |
| 136 | +// (0...self.end).get_unchecked(slice) |
| 137 | +// } |
| 138 | +// |
| 139 | +// #[inline] |
| 140 | +// fn index(self, slice: &[T]) -> &[T] { |
| 141 | +// (0...self.end).index(slice) |
| 142 | +// } |
| 143 | +// } |
0 commit comments