1 //! Indexing implementations for `[T]`.
3 use crate::intrinsics::assert_unsafe_precondition;
4 use crate::intrinsics::const_eval_select;
8 #[stable(feature = "rust1", since = "1.0.0")]
9 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
10 impl<T, I> const ops::Index<I> for [T]
12 I: ~const SliceIndex<[T]>,
14 type Output = I::Output;
17 fn index(&self, index: I) -> &I::Output {
22 #[stable(feature = "rust1", since = "1.0.0")]
23 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
24 impl<T, I> const ops::IndexMut<I> for [T]
26 I: ~const SliceIndex<[T]>,
29 fn index_mut(&mut self, index: I) -> &mut I::Output {
34 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
35 #[cfg_attr(feature = "panic_immediate_abort", inline)]
37 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
38 const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
39 // SAFETY: we are just panicking here
43 slice_start_index_len_fail_ct,
44 slice_start_index_len_fail_rt,
52 fn slice_start_index_len_fail_rt(index: usize, len: usize) -> ! {
53 panic!("range start index {index} out of range for slice of length {len}");
58 const fn slice_start_index_len_fail_ct(_: usize, _: usize) -> ! {
59 panic!("slice start index is out of range for slice");
62 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
63 #[cfg_attr(feature = "panic_immediate_abort", inline)]
65 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
66 const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
67 // SAFETY: we are just panicking here
69 const_eval_select((index, len), slice_end_index_len_fail_ct, slice_end_index_len_fail_rt)
76 fn slice_end_index_len_fail_rt(index: usize, len: usize) -> ! {
77 panic!("range end index {index} out of range for slice of length {len}");
82 const fn slice_end_index_len_fail_ct(_: usize, _: usize) -> ! {
83 panic!("slice end index is out of range for slice");
86 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
87 #[cfg_attr(feature = "panic_immediate_abort", inline)]
89 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
90 const fn slice_index_order_fail(index: usize, end: usize) -> ! {
91 // SAFETY: we are just panicking here
92 unsafe { const_eval_select((index, end), slice_index_order_fail_ct, slice_index_order_fail_rt) }
98 fn slice_index_order_fail_rt(index: usize, end: usize) -> ! {
99 panic!("slice index starts at {index} but ends at {end}");
104 const fn slice_index_order_fail_ct(_: usize, _: usize) -> ! {
105 panic!("slice index start is larger than end");
108 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
109 #[cfg_attr(feature = "panic_immediate_abort", inline)]
111 const fn slice_start_index_overflow_fail() -> ! {
112 panic!("attempted to index slice from after maximum usize");
115 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
116 #[cfg_attr(feature = "panic_immediate_abort", inline)]
118 const fn slice_end_index_overflow_fail() -> ! {
119 panic!("attempted to index slice up to maximum usize");
122 mod private_slice_index {
124 #[stable(feature = "slice_get_slice", since = "1.28.0")]
127 #[stable(feature = "slice_get_slice", since = "1.28.0")]
128 impl Sealed for usize {}
129 #[stable(feature = "slice_get_slice", since = "1.28.0")]
130 impl Sealed for ops::Range<usize> {}
131 #[stable(feature = "slice_get_slice", since = "1.28.0")]
132 impl Sealed for ops::RangeTo<usize> {}
133 #[stable(feature = "slice_get_slice", since = "1.28.0")]
134 impl Sealed for ops::RangeFrom<usize> {}
135 #[stable(feature = "slice_get_slice", since = "1.28.0")]
136 impl Sealed for ops::RangeFull {}
137 #[stable(feature = "slice_get_slice", since = "1.28.0")]
138 impl Sealed for ops::RangeInclusive<usize> {}
139 #[stable(feature = "slice_get_slice", since = "1.28.0")]
140 impl Sealed for ops::RangeToInclusive<usize> {}
141 #[stable(feature = "slice_index_with_ops_bound_pair", since = "1.53.0")]
142 impl Sealed for (ops::Bound<usize>, ops::Bound<usize>) {}
144 impl Sealed for ops::IndexRange {}
147 /// A helper trait used for indexing operations.
149 /// Implementations of this trait have to promise that if the argument
150 /// to `get_unchecked(_mut)` is a safe reference, then so is the result.
151 #[stable(feature = "slice_get_slice", since = "1.28.0")]
152 #[rustc_diagnostic_item = "SliceIndex"]
153 #[rustc_on_unimplemented(
154 on(T = "str", label = "string indices are ranges of `usize`",),
156 all(any(T = "str", T = "&str", T = "std::string::String"), _Self = "{integer}"),
157 note = "you can use `.chars().nth()` or `.bytes().nth()`\n\
158 for more information, see chapter 8 in The Book: \
159 <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
161 message = "the type `{T}` cannot be indexed by `{Self}`",
162 label = "slice indices are of type `usize` or ranges of `usize`"
165 pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
166 /// The output type returned by methods.
167 #[stable(feature = "slice_get_slice", since = "1.28.0")]
170 /// Returns a shared reference to the output at this location, if in
172 #[unstable(feature = "slice_index_methods", issue = "none")]
173 fn get(self, slice: &T) -> Option<&Self::Output>;
175 /// Returns a mutable reference to the output at this location, if in
177 #[unstable(feature = "slice_index_methods", issue = "none")]
178 fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
180 /// Returns a shared reference to the output at this location, without
181 /// performing any bounds checking.
182 /// Calling this method with an out-of-bounds index or a dangling `slice` pointer
183 /// is *[undefined behavior]* even if the resulting reference is not used.
185 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
186 #[unstable(feature = "slice_index_methods", issue = "none")]
187 unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
189 /// Returns a mutable reference to the output at this location, without
190 /// performing any bounds checking.
191 /// Calling this method with an out-of-bounds index or a dangling `slice` pointer
192 /// is *[undefined behavior]* even if the resulting reference is not used.
194 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
195 #[unstable(feature = "slice_index_methods", issue = "none")]
196 unsafe fn get_unchecked_mut(self, slice: *mut T) -> *mut Self::Output;
198 /// Returns a shared reference to the output at this location, panicking
199 /// if out of bounds.
200 #[unstable(feature = "slice_index_methods", issue = "none")]
202 fn index(self, slice: &T) -> &Self::Output;
204 /// Returns a mutable reference to the output at this location, panicking
205 /// if out of bounds.
206 #[unstable(feature = "slice_index_methods", issue = "none")]
208 fn index_mut(self, slice: &mut T) -> &mut Self::Output;
211 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
212 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
213 unsafe impl<T> const SliceIndex<[T]> for usize {
217 fn get(self, slice: &[T]) -> Option<&T> {
218 // SAFETY: `self` is checked to be in bounds.
219 if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None }
223 fn get_mut(self, slice: &mut [T]) -> Option<&mut T> {
224 // SAFETY: `self` is checked to be in bounds.
225 if self < slice.len() { unsafe { Some(&mut *self.get_unchecked_mut(slice)) } } else { None }
229 unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
231 // SAFETY: the caller guarantees that `slice` is not dangling, so it
232 // cannot be longer than `isize::MAX`. They also guarantee that
233 // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
234 // so the call to `add` is safe.
236 assert_unsafe_precondition!(
237 "slice::get_unchecked requires that the index is within the slice",
238 [T](this: usize, slice: *const [T]) => this < slice.len()
240 slice.as_ptr().add(self)
245 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
247 // SAFETY: see comments for `get_unchecked` above.
249 assert_unsafe_precondition!(
250 "slice::get_unchecked_mut requires that the index is within the slice",
251 [T](this: usize, slice: *mut [T]) => this < slice.len()
253 slice.as_mut_ptr().add(self)
258 fn index(self, slice: &[T]) -> &T {
259 // N.B., use intrinsic indexing
264 fn index_mut(self, slice: &mut [T]) -> &mut T {
265 // N.B., use intrinsic indexing
270 /// Because `IndexRange` guarantees `start <= end`, fewer checks are needed here
271 /// than there are for a general `Range<usize>` (which might be `100..3`).
272 #[rustc_const_unstable(feature = "const_index_range_slice_index", issue = "none")]
273 unsafe impl<T> const SliceIndex<[T]> for ops::IndexRange {
277 fn get(self, slice: &[T]) -> Option<&[T]> {
278 if self.end() <= slice.len() {
279 // SAFETY: `self` is checked to be valid and in bounds above.
280 unsafe { Some(&*self.get_unchecked(slice)) }
287 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
288 if self.end() <= slice.len() {
289 // SAFETY: `self` is checked to be valid and in bounds above.
290 unsafe { Some(&mut *self.get_unchecked_mut(slice)) }
297 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
298 let end = self.end();
299 // SAFETY: the caller guarantees that `slice` is not dangling, so it
300 // cannot be longer than `isize::MAX`. They also guarantee that
301 // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
302 // so the call to `add` is safe.
305 assert_unsafe_precondition!(
306 "slice::get_unchecked requires that the index is within the slice",
307 [T](end: usize, slice: *const [T]) => end <= slice.len()
309 ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len())
314 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
315 let end = self.end();
316 // SAFETY: see comments for `get_unchecked` above.
318 assert_unsafe_precondition!(
319 "slice::get_unchecked_mut requires that the index is within the slice",
320 [T](end: usize, slice: *mut [T]) => end <= slice.len()
322 ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len())
327 fn index(self, slice: &[T]) -> &[T] {
328 if self.end() <= slice.len() {
329 // SAFETY: `self` is checked to be valid and in bounds above.
330 unsafe { &*self.get_unchecked(slice) }
332 slice_end_index_len_fail(self.end(), slice.len())
337 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
338 if self.end() <= slice.len() {
339 // SAFETY: `self` is checked to be valid and in bounds above.
340 unsafe { &mut *self.get_unchecked_mut(slice) }
342 slice_end_index_len_fail(self.end(), slice.len())
347 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
348 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
349 unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
353 fn get(self, slice: &[T]) -> Option<&[T]> {
354 if self.start > self.end || self.end > slice.len() {
357 // SAFETY: `self` is checked to be valid and in bounds above.
358 unsafe { Some(&*self.get_unchecked(slice)) }
363 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
364 if self.start > self.end || self.end > slice.len() {
367 // SAFETY: `self` is checked to be valid and in bounds above.
368 unsafe { Some(&mut *self.get_unchecked_mut(slice)) }
373 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
374 let this = ops::Range { start: self.start, end: self.end };
375 // SAFETY: the caller guarantees that `slice` is not dangling, so it
376 // cannot be longer than `isize::MAX`. They also guarantee that
377 // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
378 // so the call to `add` is safe.
381 assert_unsafe_precondition!(
382 "slice::get_unchecked requires that the range is within the slice",
383 [T](this: ops::Range<usize>, slice: *const [T]) =>
384 this.end >= this.start && this.end <= slice.len()
386 ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), self.end - self.start)
391 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
392 let this = ops::Range { start: self.start, end: self.end };
393 // SAFETY: see comments for `get_unchecked` above.
395 assert_unsafe_precondition!(
396 "slice::get_unchecked_mut requires that the range is within the slice",
397 [T](this: ops::Range<usize>, slice: *mut [T]) =>
398 this.end >= this.start && this.end <= slice.len()
400 ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), self.end - self.start)
405 fn index(self, slice: &[T]) -> &[T] {
406 if self.start > self.end {
407 slice_index_order_fail(self.start, self.end);
408 } else if self.end > slice.len() {
409 slice_end_index_len_fail(self.end, slice.len());
411 // SAFETY: `self` is checked to be valid and in bounds above.
412 unsafe { &*self.get_unchecked(slice) }
416 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
417 if self.start > self.end {
418 slice_index_order_fail(self.start, self.end);
419 } else if self.end > slice.len() {
420 slice_end_index_len_fail(self.end, slice.len());
422 // SAFETY: `self` is checked to be valid and in bounds above.
423 unsafe { &mut *self.get_unchecked_mut(slice) }
427 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
428 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
429 unsafe impl<T> const SliceIndex<[T]> for ops::RangeTo<usize> {
433 fn get(self, slice: &[T]) -> Option<&[T]> {
434 (0..self.end).get(slice)
438 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
439 (0..self.end).get_mut(slice)
443 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
444 // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
445 unsafe { (0..self.end).get_unchecked(slice) }
449 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
450 // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
451 unsafe { (0..self.end).get_unchecked_mut(slice) }
455 fn index(self, slice: &[T]) -> &[T] {
456 (0..self.end).index(slice)
460 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
461 (0..self.end).index_mut(slice)
465 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
466 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
467 unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
471 fn get(self, slice: &[T]) -> Option<&[T]> {
472 (self.start..slice.len()).get(slice)
476 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
477 (self.start..slice.len()).get_mut(slice)
481 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
482 // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
483 unsafe { (self.start..slice.len()).get_unchecked(slice) }
487 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
488 // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
489 unsafe { (self.start..slice.len()).get_unchecked_mut(slice) }
493 fn index(self, slice: &[T]) -> &[T] {
494 if self.start > slice.len() {
495 slice_start_index_len_fail(self.start, slice.len());
497 // SAFETY: `self` is checked to be valid and in bounds above.
498 unsafe { &*self.get_unchecked(slice) }
502 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
503 if self.start > slice.len() {
504 slice_start_index_len_fail(self.start, slice.len());
506 // SAFETY: `self` is checked to be valid and in bounds above.
507 unsafe { &mut *self.get_unchecked_mut(slice) }
511 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
512 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
513 unsafe impl<T> const SliceIndex<[T]> for ops::RangeFull {
517 fn get(self, slice: &[T]) -> Option<&[T]> {
522 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
527 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
532 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
537 fn index(self, slice: &[T]) -> &[T] {
542 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
547 #[stable(feature = "inclusive_range", since = "1.26.0")]
548 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
549 unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
553 fn get(self, slice: &[T]) -> Option<&[T]> {
554 if *self.end() == usize::MAX { None } else { self.into_slice_range().get(slice) }
558 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
559 if *self.end() == usize::MAX { None } else { self.into_slice_range().get_mut(slice) }
563 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
564 // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
565 unsafe { self.into_slice_range().get_unchecked(slice) }
569 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
570 // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
571 unsafe { self.into_slice_range().get_unchecked_mut(slice) }
575 fn index(self, slice: &[T]) -> &[T] {
576 if *self.end() == usize::MAX {
577 slice_end_index_overflow_fail();
579 self.into_slice_range().index(slice)
583 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
584 if *self.end() == usize::MAX {
585 slice_end_index_overflow_fail();
587 self.into_slice_range().index_mut(slice)
591 #[stable(feature = "inclusive_range", since = "1.26.0")]
592 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
593 unsafe impl<T> const SliceIndex<[T]> for ops::RangeToInclusive<usize> {
597 fn get(self, slice: &[T]) -> Option<&[T]> {
598 (0..=self.end).get(slice)
602 fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
603 (0..=self.end).get_mut(slice)
607 unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
608 // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
609 unsafe { (0..=self.end).get_unchecked(slice) }
613 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
614 // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
615 unsafe { (0..=self.end).get_unchecked_mut(slice) }
619 fn index(self, slice: &[T]) -> &[T] {
620 (0..=self.end).index(slice)
624 fn index_mut(self, slice: &mut [T]) -> &mut [T] {
625 (0..=self.end).index_mut(slice)
629 /// Performs bounds-checking of a range.
631 /// This method is similar to [`Index::index`] for slices, but it returns a
632 /// [`Range`] equivalent to `range`. You can use this method to turn any range
633 /// into `start` and `end` values.
635 /// `bounds` is the range of the slice to use for bounds-checking. It should
636 /// be a [`RangeTo`] range that ends at the length of the slice.
638 /// The returned [`Range`] is safe to pass to [`slice::get_unchecked`] and
639 /// [`slice::get_unchecked_mut`] for slices with the given range.
641 /// [`Range`]: ops::Range
642 /// [`RangeTo`]: ops::RangeTo
643 /// [`slice::get_unchecked`]: slice::get_unchecked
644 /// [`slice::get_unchecked_mut`]: slice::get_unchecked_mut
648 /// Panics if `range` would be out of bounds.
653 /// #![feature(slice_range)]
657 /// let v = [10, 40, 30];
658 /// assert_eq!(1..2, slice::range(1..2, ..v.len()));
659 /// assert_eq!(0..2, slice::range(..2, ..v.len()));
660 /// assert_eq!(1..3, slice::range(1.., ..v.len()));
663 /// Panics when [`Index::index`] would panic:
666 /// #![feature(slice_range)]
670 /// let _ = slice::range(2..1, ..3);
674 /// #![feature(slice_range)]
678 /// let _ = slice::range(1..4, ..3);
682 /// #![feature(slice_range)]
686 /// let _ = slice::range(1..=usize::MAX, ..3);
689 /// [`Index::index`]: ops::Index::index
691 #[unstable(feature = "slice_range", issue = "76393")]
693 pub fn range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
695 R: ops::RangeBounds<usize>,
697 let len = bounds.end;
699 let start: ops::Bound<&usize> = range.start_bound();
700 let start = match start {
701 ops::Bound::Included(&start) => start,
702 ops::Bound::Excluded(start) => {
703 start.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
705 ops::Bound::Unbounded => 0,
708 let end: ops::Bound<&usize> = range.end_bound();
709 let end = match end {
710 ops::Bound::Included(end) => {
711 end.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
713 ops::Bound::Excluded(&end) => end,
714 ops::Bound::Unbounded => len,
718 slice_index_order_fail(start, end);
721 slice_end_index_len_fail(end, len);
724 ops::Range { start, end }
727 /// Convert pair of `ops::Bound`s into `ops::Range` without performing any bounds checking and (in debug) overflow checking
728 fn into_range_unchecked(
730 (start, end): (ops::Bound<usize>, ops::Bound<usize>),
731 ) -> ops::Range<usize> {
733 let start = match start {
734 Bound::Included(i) => i,
735 Bound::Excluded(i) => i + 1,
736 Bound::Unbounded => 0,
738 let end = match end {
739 Bound::Included(i) => i + 1,
740 Bound::Excluded(i) => i,
741 Bound::Unbounded => len,
746 /// Convert pair of `ops::Bound`s into `ops::Range`.
747 /// Returns `None` on overflowing indices.
750 (start, end): (ops::Bound<usize>, ops::Bound<usize>),
751 ) -> Option<ops::Range<usize>> {
753 let start = match start {
754 Bound::Included(start) => start,
755 Bound::Excluded(start) => start.checked_add(1)?,
756 Bound::Unbounded => 0,
759 let end = match end {
760 Bound::Included(end) => end.checked_add(1)?,
761 Bound::Excluded(end) => end,
762 Bound::Unbounded => len,
765 // Don't bother with checking `start < end` and `end <= len`
766 // since these checks are handled by `Range` impls
771 /// Convert pair of `ops::Bound`s into `ops::Range`.
772 /// Panics on overflowing indices.
775 (start, end): (ops::Bound<usize>, ops::Bound<usize>),
776 ) -> ops::Range<usize> {
778 let start = match start {
779 Bound::Included(start) => start,
780 Bound::Excluded(start) => {
781 start.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
783 Bound::Unbounded => 0,
786 let end = match end {
787 Bound::Included(end) => {
788 end.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
790 Bound::Excluded(end) => end,
791 Bound::Unbounded => len,
794 // Don't bother with checking `start < end` and `end <= len`
795 // since these checks are handled by `Range` impls
800 #[stable(feature = "slice_index_with_ops_bound_pair", since = "1.53.0")]
801 unsafe impl<T> SliceIndex<[T]> for (ops::Bound<usize>, ops::Bound<usize>) {
805 fn get(self, slice: &[T]) -> Option<&Self::Output> {
806 into_range(slice.len(), self)?.get(slice)
810 fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output> {
811 into_range(slice.len(), self)?.get_mut(slice)
815 unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output {
816 // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
817 unsafe { into_range_unchecked(slice.len(), self).get_unchecked(slice) }
821 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output {
822 // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
823 unsafe { into_range_unchecked(slice.len(), self).get_unchecked_mut(slice) }
827 fn index(self, slice: &[T]) -> &Self::Output {
828 into_slice_range(slice.len(), self).index(slice)
832 fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
833 into_slice_range(slice.len(), self).index_mut(slice)