]> git.lizzy.rs Git - rust.git/blob - library/core/src/slice/index.rs
Rollup merge of #95553 - jam1garner:naked-function-compile-error, r=tmiasko
[rust.git] / library / core / src / slice / index.rs
1 //! Indexing implementations for `[T]`.
2
3 use crate::intrinsics::assert_unsafe_precondition;
4 use crate::intrinsics::const_eval_select;
5 use crate::ops;
6 use crate::ptr;
7
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]
11 where
12     I: ~const SliceIndex<[T]>,
13 {
14     type Output = I::Output;
15
16     #[inline]
17     fn index(&self, index: I) -> &I::Output {
18         index.index(self)
19     }
20 }
21
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]
25 where
26     I: ~const SliceIndex<[T]>,
27 {
28     #[inline]
29     fn index_mut(&mut self, index: I) -> &mut I::Output {
30         index.index_mut(self)
31     }
32 }
33
34 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
35 #[cfg_attr(feature = "panic_immediate_abort", inline)]
36 #[cold]
37 #[track_caller]
38 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
39 const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
40     // SAFETY: we are just panicking here
41     unsafe {
42         const_eval_select(
43             (index, len),
44             slice_start_index_len_fail_ct,
45             slice_start_index_len_fail_rt,
46         )
47     }
48 }
49
50 // FIXME const-hack
51 fn slice_start_index_len_fail_rt(index: usize, len: usize) -> ! {
52     panic!("range start index {index} out of range for slice of length {len}");
53 }
54
55 const fn slice_start_index_len_fail_ct(_: usize, _: usize) -> ! {
56     panic!("slice start index is out of range for slice");
57 }
58
59 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
60 #[cfg_attr(feature = "panic_immediate_abort", inline)]
61 #[cold]
62 #[track_caller]
63 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
64 const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
65     // SAFETY: we are just panicking here
66     unsafe {
67         const_eval_select((index, len), slice_end_index_len_fail_ct, slice_end_index_len_fail_rt)
68     }
69 }
70
71 // FIXME const-hack
72 fn slice_end_index_len_fail_rt(index: usize, len: usize) -> ! {
73     panic!("range end index {index} out of range for slice of length {len}");
74 }
75
76 const fn slice_end_index_len_fail_ct(_: usize, _: usize) -> ! {
77     panic!("slice end index is out of range for slice");
78 }
79
80 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
81 #[cfg_attr(feature = "panic_immediate_abort", inline)]
82 #[cold]
83 #[track_caller]
84 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
85 const fn slice_index_order_fail(index: usize, end: usize) -> ! {
86     // SAFETY: we are just panicking here
87     unsafe { const_eval_select((index, end), slice_index_order_fail_ct, slice_index_order_fail_rt) }
88 }
89
90 // FIXME const-hack
91 fn slice_index_order_fail_rt(index: usize, end: usize) -> ! {
92     panic!("slice index starts at {index} but ends at {end}");
93 }
94
95 const fn slice_index_order_fail_ct(_: usize, _: usize) -> ! {
96     panic!("slice index start is larger than end");
97 }
98
99 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
100 #[cfg_attr(feature = "panic_immediate_abort", inline)]
101 #[cold]
102 #[track_caller]
103 const fn slice_start_index_overflow_fail() -> ! {
104     panic!("attempted to index slice from after maximum usize");
105 }
106
107 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
108 #[cfg_attr(feature = "panic_immediate_abort", inline)]
109 #[cold]
110 #[track_caller]
111 const fn slice_end_index_overflow_fail() -> ! {
112     panic!("attempted to index slice up to maximum usize");
113 }
114
115 mod private_slice_index {
116     use super::ops;
117     #[stable(feature = "slice_get_slice", since = "1.28.0")]
118     pub trait Sealed {}
119
120     #[stable(feature = "slice_get_slice", since = "1.28.0")]
121     impl Sealed for usize {}
122     #[stable(feature = "slice_get_slice", since = "1.28.0")]
123     impl Sealed for ops::Range<usize> {}
124     #[stable(feature = "slice_get_slice", since = "1.28.0")]
125     impl Sealed for ops::RangeTo<usize> {}
126     #[stable(feature = "slice_get_slice", since = "1.28.0")]
127     impl Sealed for ops::RangeFrom<usize> {}
128     #[stable(feature = "slice_get_slice", since = "1.28.0")]
129     impl Sealed for ops::RangeFull {}
130     #[stable(feature = "slice_get_slice", since = "1.28.0")]
131     impl Sealed for ops::RangeInclusive<usize> {}
132     #[stable(feature = "slice_get_slice", since = "1.28.0")]
133     impl Sealed for ops::RangeToInclusive<usize> {}
134     #[stable(feature = "slice_index_with_ops_bound_pair", since = "1.53.0")]
135     impl Sealed for (ops::Bound<usize>, ops::Bound<usize>) {}
136 }
137
138 /// A helper trait used for indexing operations.
139 ///
140 /// Implementations of this trait have to promise that if the argument
141 /// to `get_(mut_)unchecked` is a safe reference, then so is the result.
142 #[stable(feature = "slice_get_slice", since = "1.28.0")]
143 #[rustc_on_unimplemented(
144     on(T = "str", label = "string indices are ranges of `usize`",),
145     on(
146         all(any(T = "str", T = "&str", T = "std::string::String"), _Self = "{integer}"),
147         note = "you can use `.chars().nth()` or `.bytes().nth()`\n\
148                 for more information, see chapter 8 in The Book: \
149                 <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
150     ),
151     message = "the type `{T}` cannot be indexed by `{Self}`",
152     label = "slice indices are of type `usize` or ranges of `usize`"
153 )]
154 pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
155     /// The output type returned by methods.
156     #[stable(feature = "slice_get_slice", since = "1.28.0")]
157     type Output: ?Sized;
158
159     /// Returns a shared reference to the output at this location, if in
160     /// bounds.
161     #[unstable(feature = "slice_index_methods", issue = "none")]
162     fn get(self, slice: &T) -> Option<&Self::Output>;
163
164     /// Returns a mutable reference to the output at this location, if in
165     /// bounds.
166     #[unstable(feature = "slice_index_methods", issue = "none")]
167     fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
168
169     /// Returns a shared reference to the output at this location, without
170     /// performing any bounds checking.
171     /// Calling this method with an out-of-bounds index or a dangling `slice` pointer
172     /// is *[undefined behavior]* even if the resulting reference is not used.
173     ///
174     /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
175     #[unstable(feature = "slice_index_methods", issue = "none")]
176     unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
177
178     /// Returns a mutable reference to the output at this location, without
179     /// performing any bounds checking.
180     /// Calling this method with an out-of-bounds index or a dangling `slice` pointer
181     /// is *[undefined behavior]* even if the resulting reference is not used.
182     ///
183     /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
184     #[unstable(feature = "slice_index_methods", issue = "none")]
185     unsafe fn get_unchecked_mut(self, slice: *mut T) -> *mut Self::Output;
186
187     /// Returns a shared reference to the output at this location, panicking
188     /// if out of bounds.
189     #[unstable(feature = "slice_index_methods", issue = "none")]
190     #[track_caller]
191     fn index(self, slice: &T) -> &Self::Output;
192
193     /// Returns a mutable reference to the output at this location, panicking
194     /// if out of bounds.
195     #[unstable(feature = "slice_index_methods", issue = "none")]
196     #[track_caller]
197     fn index_mut(self, slice: &mut T) -> &mut Self::Output;
198 }
199
200 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
201 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
202 unsafe impl<T> const SliceIndex<[T]> for usize {
203     type Output = T;
204
205     #[inline]
206     fn get(self, slice: &[T]) -> Option<&T> {
207         // SAFETY: `self` is checked to be in bounds.
208         if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None }
209     }
210
211     #[inline]
212     fn get_mut(self, slice: &mut [T]) -> Option<&mut T> {
213         // SAFETY: `self` is checked to be in bounds.
214         if self < slice.len() { unsafe { Some(&mut *self.get_unchecked_mut(slice)) } } else { None }
215     }
216
217     #[inline]
218     unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
219         // SAFETY: the caller guarantees that `slice` is not dangling, so it
220         // cannot be longer than `isize::MAX`. They also guarantee that
221         // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
222         // so the call to `add` is safe.
223         unsafe {
224             assert_unsafe_precondition!(self < slice.len());
225             slice.as_ptr().add(self)
226         }
227     }
228
229     #[inline]
230     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
231         // SAFETY: see comments for `get_unchecked` above.
232         unsafe {
233             assert_unsafe_precondition!(self < slice.len());
234             slice.as_mut_ptr().add(self)
235         }
236     }
237
238     #[inline]
239     fn index(self, slice: &[T]) -> &T {
240         // N.B., use intrinsic indexing
241         &(*slice)[self]
242     }
243
244     #[inline]
245     fn index_mut(self, slice: &mut [T]) -> &mut T {
246         // N.B., use intrinsic indexing
247         &mut (*slice)[self]
248     }
249 }
250
251 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
252 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
253 unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
254     type Output = [T];
255
256     #[inline]
257     fn get(self, slice: &[T]) -> Option<&[T]> {
258         if self.start > self.end || self.end > slice.len() {
259             None
260         } else {
261             // SAFETY: `self` is checked to be valid and in bounds above.
262             unsafe { Some(&*self.get_unchecked(slice)) }
263         }
264     }
265
266     #[inline]
267     fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
268         if self.start > self.end || self.end > slice.len() {
269             None
270         } else {
271             // SAFETY: `self` is checked to be valid and in bounds above.
272             unsafe { Some(&mut *self.get_unchecked_mut(slice)) }
273         }
274     }
275
276     #[inline]
277     unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
278         // SAFETY: the caller guarantees that `slice` is not dangling, so it
279         // cannot be longer than `isize::MAX`. They also guarantee that
280         // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
281         // so the call to `add` is safe.
282
283         unsafe {
284             assert_unsafe_precondition!(self.end >= self.start && self.end <= slice.len());
285             ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), self.end - self.start)
286         }
287     }
288
289     #[inline]
290     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
291         // SAFETY: see comments for `get_unchecked` above.
292         unsafe {
293             assert_unsafe_precondition!(self.end >= self.start && self.end <= slice.len());
294             ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), self.end - self.start)
295         }
296     }
297
298     #[inline]
299     fn index(self, slice: &[T]) -> &[T] {
300         if self.start > self.end {
301             slice_index_order_fail(self.start, self.end);
302         } else if self.end > slice.len() {
303             slice_end_index_len_fail(self.end, slice.len());
304         }
305         // SAFETY: `self` is checked to be valid and in bounds above.
306         unsafe { &*self.get_unchecked(slice) }
307     }
308
309     #[inline]
310     fn index_mut(self, slice: &mut [T]) -> &mut [T] {
311         if self.start > self.end {
312             slice_index_order_fail(self.start, self.end);
313         } else if self.end > slice.len() {
314             slice_end_index_len_fail(self.end, slice.len());
315         }
316         // SAFETY: `self` is checked to be valid and in bounds above.
317         unsafe { &mut *self.get_unchecked_mut(slice) }
318     }
319 }
320
321 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
322 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
323 unsafe impl<T> const SliceIndex<[T]> for ops::RangeTo<usize> {
324     type Output = [T];
325
326     #[inline]
327     fn get(self, slice: &[T]) -> Option<&[T]> {
328         (0..self.end).get(slice)
329     }
330
331     #[inline]
332     fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
333         (0..self.end).get_mut(slice)
334     }
335
336     #[inline]
337     unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
338         // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
339         unsafe { (0..self.end).get_unchecked(slice) }
340     }
341
342     #[inline]
343     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
344         // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
345         unsafe { (0..self.end).get_unchecked_mut(slice) }
346     }
347
348     #[inline]
349     fn index(self, slice: &[T]) -> &[T] {
350         (0..self.end).index(slice)
351     }
352
353     #[inline]
354     fn index_mut(self, slice: &mut [T]) -> &mut [T] {
355         (0..self.end).index_mut(slice)
356     }
357 }
358
359 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
360 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
361 unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
362     type Output = [T];
363
364     #[inline]
365     fn get(self, slice: &[T]) -> Option<&[T]> {
366         (self.start..slice.len()).get(slice)
367     }
368
369     #[inline]
370     fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
371         (self.start..slice.len()).get_mut(slice)
372     }
373
374     #[inline]
375     unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
376         // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
377         unsafe { (self.start..slice.len()).get_unchecked(slice) }
378     }
379
380     #[inline]
381     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
382         // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
383         unsafe { (self.start..slice.len()).get_unchecked_mut(slice) }
384     }
385
386     #[inline]
387     fn index(self, slice: &[T]) -> &[T] {
388         if self.start > slice.len() {
389             slice_start_index_len_fail(self.start, slice.len());
390         }
391         // SAFETY: `self` is checked to be valid and in bounds above.
392         unsafe { &*self.get_unchecked(slice) }
393     }
394
395     #[inline]
396     fn index_mut(self, slice: &mut [T]) -> &mut [T] {
397         if self.start > slice.len() {
398             slice_start_index_len_fail(self.start, slice.len());
399         }
400         // SAFETY: `self` is checked to be valid and in bounds above.
401         unsafe { &mut *self.get_unchecked_mut(slice) }
402     }
403 }
404
405 #[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
406 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
407 unsafe impl<T> const SliceIndex<[T]> for ops::RangeFull {
408     type Output = [T];
409
410     #[inline]
411     fn get(self, slice: &[T]) -> Option<&[T]> {
412         Some(slice)
413     }
414
415     #[inline]
416     fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
417         Some(slice)
418     }
419
420     #[inline]
421     unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
422         slice
423     }
424
425     #[inline]
426     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
427         slice
428     }
429
430     #[inline]
431     fn index(self, slice: &[T]) -> &[T] {
432         slice
433     }
434
435     #[inline]
436     fn index_mut(self, slice: &mut [T]) -> &mut [T] {
437         slice
438     }
439 }
440
441 #[stable(feature = "inclusive_range", since = "1.26.0")]
442 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
443 unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
444     type Output = [T];
445
446     #[inline]
447     fn get(self, slice: &[T]) -> Option<&[T]> {
448         if *self.end() == usize::MAX { None } else { self.into_slice_range().get(slice) }
449     }
450
451     #[inline]
452     fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
453         if *self.end() == usize::MAX { None } else { self.into_slice_range().get_mut(slice) }
454     }
455
456     #[inline]
457     unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
458         // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
459         unsafe { self.into_slice_range().get_unchecked(slice) }
460     }
461
462     #[inline]
463     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
464         // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
465         unsafe { self.into_slice_range().get_unchecked_mut(slice) }
466     }
467
468     #[inline]
469     fn index(self, slice: &[T]) -> &[T] {
470         if *self.end() == usize::MAX {
471             slice_end_index_overflow_fail();
472         }
473         self.into_slice_range().index(slice)
474     }
475
476     #[inline]
477     fn index_mut(self, slice: &mut [T]) -> &mut [T] {
478         if *self.end() == usize::MAX {
479             slice_end_index_overflow_fail();
480         }
481         self.into_slice_range().index_mut(slice)
482     }
483 }
484
485 #[stable(feature = "inclusive_range", since = "1.26.0")]
486 #[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
487 unsafe impl<T> const SliceIndex<[T]> for ops::RangeToInclusive<usize> {
488     type Output = [T];
489
490     #[inline]
491     fn get(self, slice: &[T]) -> Option<&[T]> {
492         (0..=self.end).get(slice)
493     }
494
495     #[inline]
496     fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
497         (0..=self.end).get_mut(slice)
498     }
499
500     #[inline]
501     unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
502         // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
503         unsafe { (0..=self.end).get_unchecked(slice) }
504     }
505
506     #[inline]
507     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
508         // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
509         unsafe { (0..=self.end).get_unchecked_mut(slice) }
510     }
511
512     #[inline]
513     fn index(self, slice: &[T]) -> &[T] {
514         (0..=self.end).index(slice)
515     }
516
517     #[inline]
518     fn index_mut(self, slice: &mut [T]) -> &mut [T] {
519         (0..=self.end).index_mut(slice)
520     }
521 }
522
523 /// Performs bounds-checking of a range.
524 ///
525 /// This method is similar to [`Index::index`] for slices, but it returns a
526 /// [`Range`] equivalent to `range`. You can use this method to turn any range
527 /// into `start` and `end` values.
528 ///
529 /// `bounds` is the range of the slice to use for bounds-checking. It should
530 /// be a [`RangeTo`] range that ends at the length of the slice.
531 ///
532 /// The returned [`Range`] is safe to pass to [`slice::get_unchecked`] and
533 /// [`slice::get_unchecked_mut`] for slices with the given range.
534 ///
535 /// [`Range`]: ops::Range
536 /// [`RangeTo`]: ops::RangeTo
537 /// [`slice::get_unchecked`]: slice::get_unchecked
538 /// [`slice::get_unchecked_mut`]: slice::get_unchecked_mut
539 ///
540 /// # Panics
541 ///
542 /// Panics if `range` would be out of bounds.
543 ///
544 /// # Examples
545 ///
546 /// ```
547 /// #![feature(slice_range)]
548 ///
549 /// use std::slice;
550 ///
551 /// let v = [10, 40, 30];
552 /// assert_eq!(1..2, slice::range(1..2, ..v.len()));
553 /// assert_eq!(0..2, slice::range(..2, ..v.len()));
554 /// assert_eq!(1..3, slice::range(1.., ..v.len()));
555 /// ```
556 ///
557 /// Panics when [`Index::index`] would panic:
558 ///
559 /// ```should_panic
560 /// #![feature(slice_range)]
561 ///
562 /// use std::slice;
563 ///
564 /// let _ = slice::range(2..1, ..3);
565 /// ```
566 ///
567 /// ```should_panic
568 /// #![feature(slice_range)]
569 ///
570 /// use std::slice;
571 ///
572 /// let _ = slice::range(1..4, ..3);
573 /// ```
574 ///
575 /// ```should_panic
576 /// #![feature(slice_range)]
577 ///
578 /// use std::slice;
579 ///
580 /// let _ = slice::range(1..=usize::MAX, ..3);
581 /// ```
582 ///
583 /// [`Index::index`]: ops::Index::index
584 #[track_caller]
585 #[unstable(feature = "slice_range", issue = "76393")]
586 #[must_use]
587 pub fn range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
588 where
589     R: ops::RangeBounds<usize>,
590 {
591     let len = bounds.end;
592
593     let start: ops::Bound<&usize> = range.start_bound();
594     let start = match start {
595         ops::Bound::Included(&start) => start,
596         ops::Bound::Excluded(start) => {
597             start.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
598         }
599         ops::Bound::Unbounded => 0,
600     };
601
602     let end: ops::Bound<&usize> = range.end_bound();
603     let end = match end {
604         ops::Bound::Included(end) => {
605             end.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
606         }
607         ops::Bound::Excluded(&end) => end,
608         ops::Bound::Unbounded => len,
609     };
610
611     if start > end {
612         slice_index_order_fail(start, end);
613     }
614     if end > len {
615         slice_end_index_len_fail(end, len);
616     }
617
618     ops::Range { start, end }
619 }
620
621 /// Convert pair of `ops::Bound`s into `ops::Range` without performing any bounds checking and (in debug) overflow checking
622 fn into_range_unchecked(
623     len: usize,
624     (start, end): (ops::Bound<usize>, ops::Bound<usize>),
625 ) -> ops::Range<usize> {
626     use ops::Bound;
627     let start = match start {
628         Bound::Included(i) => i,
629         Bound::Excluded(i) => i + 1,
630         Bound::Unbounded => 0,
631     };
632     let end = match end {
633         Bound::Included(i) => i + 1,
634         Bound::Excluded(i) => i,
635         Bound::Unbounded => len,
636     };
637     start..end
638 }
639
640 /// Convert pair of `ops::Bound`s into `ops::Range`.
641 /// Returns `None` on overflowing indices.
642 fn into_range(
643     len: usize,
644     (start, end): (ops::Bound<usize>, ops::Bound<usize>),
645 ) -> Option<ops::Range<usize>> {
646     use ops::Bound;
647     let start = match start {
648         Bound::Included(start) => start,
649         Bound::Excluded(start) => start.checked_add(1)?,
650         Bound::Unbounded => 0,
651     };
652
653     let end = match end {
654         Bound::Included(end) => end.checked_add(1)?,
655         Bound::Excluded(end) => end,
656         Bound::Unbounded => len,
657     };
658
659     // Don't bother with checking `start < end` and `end <= len`
660     // since these checks are handled by `Range` impls
661
662     Some(start..end)
663 }
664
665 /// Convert pair of `ops::Bound`s into `ops::Range`.
666 /// Panics on overflowing indices.
667 fn into_slice_range(
668     len: usize,
669     (start, end): (ops::Bound<usize>, ops::Bound<usize>),
670 ) -> ops::Range<usize> {
671     use ops::Bound;
672     let start = match start {
673         Bound::Included(start) => start,
674         Bound::Excluded(start) => {
675             start.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
676         }
677         Bound::Unbounded => 0,
678     };
679
680     let end = match end {
681         Bound::Included(end) => {
682             end.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
683         }
684         Bound::Excluded(end) => end,
685         Bound::Unbounded => len,
686     };
687
688     // Don't bother with checking `start < end` and `end <= len`
689     // since these checks are handled by `Range` impls
690
691     start..end
692 }
693
694 #[stable(feature = "slice_index_with_ops_bound_pair", since = "1.53.0")]
695 unsafe impl<T> SliceIndex<[T]> for (ops::Bound<usize>, ops::Bound<usize>) {
696     type Output = [T];
697
698     #[inline]
699     fn get(self, slice: &[T]) -> Option<&Self::Output> {
700         into_range(slice.len(), self)?.get(slice)
701     }
702
703     #[inline]
704     fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output> {
705         into_range(slice.len(), self)?.get_mut(slice)
706     }
707
708     #[inline]
709     unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output {
710         // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
711         unsafe { into_range_unchecked(slice.len(), self).get_unchecked(slice) }
712     }
713
714     #[inline]
715     unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output {
716         // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
717         unsafe { into_range_unchecked(slice.len(), self).get_unchecked_mut(slice) }
718     }
719
720     #[inline]
721     fn index(self, slice: &[T]) -> &Self::Output {
722         into_slice_range(slice.len(), self).index(slice)
723     }
724
725     #[inline]
726     fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
727         into_slice_range(slice.len(), self).index_mut(slice)
728     }
729 }