core/slice/
index.rs

1//! Indexing implementations for `[T]`.
2
3use crate::intrinsics::slice_get_unchecked;
4#[cfg(not(feature = "ferrocene_certified"))]
5use crate::marker::Destruct;
6use crate::panic::const_panic;
7use crate::ub_checks::assert_unsafe_precondition;
8#[cfg(not(feature = "ferrocene_certified"))]
9use crate::{ops, range};
10
11// Ferrocene addition: imports for certified subset
12#[cfg(feature = "ferrocene_certified")]
13#[rustfmt::skip]
14use crate::ops;
15
16#[stable(feature = "rust1", since = "1.0.0")]
17#[rustc_const_unstable(feature = "const_index", issue = "143775")]
18impl<T, I> const ops::Index<I> for [T]
19where
20    I: [const] SliceIndex<[T]>,
21{
22    type Output = I::Output;
23
24    #[inline(always)]
25    fn index(&self, index: I) -> &I::Output {
26        index.index(self)
27    }
28}
29
30#[stable(feature = "rust1", since = "1.0.0")]
31#[rustc_const_unstable(feature = "const_index", issue = "143775")]
32impl<T, I> const ops::IndexMut<I> for [T]
33where
34    I: [const] SliceIndex<[T]>,
35{
36    #[inline(always)]
37    fn index_mut(&mut self, index: I) -> &mut I::Output {
38        index.index_mut(self)
39    }
40}
41
42#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
43#[cfg_attr(panic = "immediate-abort", inline)]
44#[track_caller]
45const fn slice_index_fail(start: usize, end: usize, len: usize) -> ! {
46    if start > len {
47        const_panic!(
48            "slice start index is out of range for slice",
49            "range start index {start} out of range for slice of length {len}",
50            start: usize,
51            len: usize,
52        )
53    }
54
55    if end > len {
56        const_panic!(
57            "slice end index is out of range for slice",
58            "range end index {end} out of range for slice of length {len}",
59            end: usize,
60            len: usize,
61        )
62    }
63
64    if start > end {
65        const_panic!(
66            "slice index start is larger than end",
67            "slice index starts at {start} but ends at {end}",
68            start: usize,
69            end: usize,
70        )
71    }
72
73    // Only reachable if the range was a `RangeInclusive` or a
74    // `RangeToInclusive`, with `end == len`.
75    const_panic!(
76        "slice end index is out of range for slice",
77        "range end index {end} out of range for slice of length {len}",
78        end: usize,
79        len: usize,
80    )
81}
82
83// The UbChecks are great for catching bugs in the unsafe methods, but including
84// them in safe indexing is unnecessary and hurts inlining and debug runtime perf.
85// Both the safe and unsafe public methods share these helpers,
86// which use intrinsics directly to get *no* extra checks.
87
88#[inline(always)]
89const unsafe fn get_offset_len_noubcheck<T>(
90    ptr: *const [T],
91    offset: usize,
92    len: usize,
93) -> *const [T] {
94    let ptr = ptr as *const T;
95    // SAFETY: The caller already checked these preconditions
96    let ptr = unsafe { crate::intrinsics::offset(ptr, offset) };
97    crate::intrinsics::aggregate_raw_ptr(ptr, len)
98}
99
100#[inline(always)]
101const unsafe fn get_offset_len_mut_noubcheck<T>(
102    ptr: *mut [T],
103    offset: usize,
104    len: usize,
105) -> *mut [T] {
106    let ptr = ptr as *mut T;
107    // SAFETY: The caller already checked these preconditions
108    let ptr = unsafe { crate::intrinsics::offset(ptr, offset) };
109    crate::intrinsics::aggregate_raw_ptr(ptr, len)
110}
111
112mod private_slice_index {
113    #[cfg(not(feature = "ferrocene_certified"))]
114    use super::{ops, range};
115
116    // Ferrocene addition: imports for certified subset
117    #[cfg(feature = "ferrocene_certified")]
118    #[rustfmt::skip]
119    use super::ops;
120
121    #[stable(feature = "slice_get_slice", since = "1.28.0")]
122    pub trait Sealed {}
123
124    #[stable(feature = "slice_get_slice", since = "1.28.0")]
125    impl Sealed for usize {}
126    #[stable(feature = "slice_get_slice", since = "1.28.0")]
127    impl Sealed for ops::Range<usize> {}
128    #[stable(feature = "slice_get_slice", since = "1.28.0")]
129    impl Sealed for ops::RangeTo<usize> {}
130    #[stable(feature = "slice_get_slice", since = "1.28.0")]
131    impl Sealed for ops::RangeFrom<usize> {}
132    #[stable(feature = "slice_get_slice", since = "1.28.0")]
133    impl Sealed for ops::RangeFull {}
134    #[stable(feature = "slice_get_slice", since = "1.28.0")]
135    impl Sealed for ops::RangeInclusive<usize> {}
136    #[stable(feature = "slice_get_slice", since = "1.28.0")]
137    impl Sealed for ops::RangeToInclusive<usize> {}
138    #[stable(feature = "slice_index_with_ops_bound_pair", since = "1.53.0")]
139    impl Sealed for (ops::Bound<usize>, ops::Bound<usize>) {}
140
141    #[unstable(feature = "new_range_api", issue = "125687")]
142    #[cfg(not(feature = "ferrocene_certified"))]
143    impl Sealed for range::Range<usize> {}
144    #[unstable(feature = "new_range_api", issue = "125687")]
145    #[cfg(not(feature = "ferrocene_certified"))]
146    impl Sealed for range::RangeInclusive<usize> {}
147    #[unstable(feature = "new_range_api", issue = "125687")]
148    #[cfg(not(feature = "ferrocene_certified"))]
149    impl Sealed for range::RangeToInclusive<usize> {}
150    #[unstable(feature = "new_range_api", issue = "125687")]
151    #[cfg(not(feature = "ferrocene_certified"))]
152    impl Sealed for range::RangeFrom<usize> {}
153
154    impl Sealed for ops::IndexRange {}
155
156    #[cfg(not(feature = "ferrocene_certified"))]
157    #[unstable(feature = "sliceindex_wrappers", issue = "146179")]
158    impl Sealed for crate::index::Last {}
159    #[cfg(not(feature = "ferrocene_certified"))]
160    #[unstable(feature = "sliceindex_wrappers", issue = "146179")]
161    impl<T> Sealed for crate::index::Clamp<T> where T: Sealed {}
162}
163
164/// A helper trait used for indexing operations.
165///
166/// Implementations of this trait have to promise that if the argument
167/// to `get_unchecked(_mut)` is a safe reference, then so is the result.
168#[stable(feature = "slice_get_slice", since = "1.28.0")]
169#[rustc_diagnostic_item = "SliceIndex"]
170#[rustc_on_unimplemented(
171    on(T = "str", label = "string indices are ranges of `usize`",),
172    on(
173        all(any(T = "str", T = "&str", T = "alloc::string::String"), Self = "{integer}"),
174        note = "you can use `.chars().nth()` or `.bytes().nth()`\n\
175                for more information, see chapter 8 in The Book: \
176                <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
177    ),
178    message = "the type `{T}` cannot be indexed by `{Self}`",
179    label = "slice indices are of type `usize` or ranges of `usize`"
180)]
181#[const_trait] // FIXME(const_trait_impl): Migrate to `const unsafe trait` once #146122 is fixed.
182#[rustc_const_unstable(feature = "const_index", issue = "143775")]
183pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
184    /// The output type returned by methods.
185    #[stable(feature = "slice_get_slice", since = "1.28.0")]
186    type Output: ?Sized;
187
188    /// Returns a shared reference to the output at this location, if in
189    /// bounds.
190    #[unstable(feature = "slice_index_methods", issue = "none")]
191    fn get(self, slice: &T) -> Option<&Self::Output>;
192
193    /// Returns a mutable reference to the output at this location, if in
194    /// bounds.
195    #[unstable(feature = "slice_index_methods", issue = "none")]
196    fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
197
198    /// Returns a pointer to the output at this location, without
199    /// performing any bounds checking.
200    ///
201    /// Calling this method with an out-of-bounds index or a dangling `slice` pointer
202    /// is *[undefined behavior]* even if the resulting pointer is not used.
203    ///
204    /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
205    #[unstable(feature = "slice_index_methods", issue = "none")]
206    unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
207
208    /// Returns a mutable pointer to the output at this location, without
209    /// performing any bounds checking.
210    ///
211    /// Calling this method with an out-of-bounds index or a dangling `slice` pointer
212    /// is *[undefined behavior]* even if the resulting pointer is not used.
213    ///
214    /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
215    #[unstable(feature = "slice_index_methods", issue = "none")]
216    unsafe fn get_unchecked_mut(self, slice: *mut T) -> *mut Self::Output;
217
218    /// Returns a shared reference to the output at this location, panicking
219    /// if out of bounds.
220    #[unstable(feature = "slice_index_methods", issue = "none")]
221    #[track_caller]
222    fn index(self, slice: &T) -> &Self::Output;
223
224    /// Returns a mutable reference to the output at this location, panicking
225    /// if out of bounds.
226    #[unstable(feature = "slice_index_methods", issue = "none")]
227    #[track_caller]
228    fn index_mut(self, slice: &mut T) -> &mut Self::Output;
229}
230
231/// The methods `index` and `index_mut` panic if the index is out of bounds.
232#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
233#[rustc_const_unstable(feature = "const_index", issue = "143775")]
234unsafe impl<T> const SliceIndex<[T]> for usize {
235    type Output = T;
236
237    #[inline]
238    fn get(self, slice: &[T]) -> Option<&T> {
239        if self < slice.len() {
240            // SAFETY: `self` is checked to be in bounds.
241            unsafe { Some(slice_get_unchecked(slice, self)) }
242        } else {
243            None
244        }
245    }
246
247    #[inline]
248    fn get_mut(self, slice: &mut [T]) -> Option<&mut T> {
249        if self < slice.len() {
250            // SAFETY: `self` is checked to be in bounds.
251            unsafe { Some(slice_get_unchecked(slice, self)) }
252        } else {
253            None
254        }
255    }
256
257    #[inline]
258    #[track_caller]
259    unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
260        assert_unsafe_precondition!(
261            check_language_ub, // okay because of the `assume` below
262            "slice::get_unchecked requires that the index is within the slice",
263            (this: usize = self, len: usize = slice.len()) => this < len
264        );
265        // SAFETY: the caller guarantees that `slice` is not dangling, so it
266        // cannot be longer than `isize::MAX`. They also guarantee that
267        // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
268        // so the call to `add` is safe.
269        unsafe {
270            // Use intrinsics::assume instead of hint::assert_unchecked so that we don't check the
271            // precondition of this function twice.
272            crate::intrinsics::assume(self < slice.len());
273            slice_get_unchecked(slice, self)
274        }
275    }
276
277    #[inline]
278    #[track_caller]
279    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
280        assert_unsafe_precondition!(
281            check_library_ub,
282            "slice::get_unchecked_mut requires that the index is within the slice",
283            (this: usize = self, len: usize = slice.len()) => this < len
284        );
285        // SAFETY: see comments for `get_unchecked` above.
286        unsafe { slice_get_unchecked(slice, self) }
287    }
288
289    #[inline]
290    fn index(self, slice: &[T]) -> &T {
291        // N.B., use intrinsic indexing
292        &(*slice)[self]
293    }
294
295    #[inline]
296    fn index_mut(self, slice: &mut [T]) -> &mut T {
297        // N.B., use intrinsic indexing
298        &mut (*slice)[self]
299    }
300}
301
302/// Because `IndexRange` guarantees `start <= end`, fewer checks are needed here
303/// than there are for a general `Range<usize>` (which might be `100..3`).
304#[rustc_const_unstable(feature = "const_index", issue = "143775")]
305unsafe impl<T> const SliceIndex<[T]> for ops::IndexRange {
306    type Output = [T];
307
308    #[inline]
309    fn get(self, slice: &[T]) -> Option<&[T]> {
310        if self.end() <= slice.len() {
311            // SAFETY: `self` is checked to be valid and in bounds above.
312            unsafe { Some(&*get_offset_len_noubcheck(slice, self.start(), self.len())) }
313        } else {
314            None
315        }
316    }
317
318    #[inline]
319    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
320        if self.end() <= slice.len() {
321            // SAFETY: `self` is checked to be valid and in bounds above.
322            unsafe { Some(&mut *get_offset_len_mut_noubcheck(slice, self.start(), self.len())) }
323        } else {
324            None
325        }
326    }
327
328    #[inline]
329    #[track_caller]
330    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
331        assert_unsafe_precondition!(
332            check_library_ub,
333            "slice::get_unchecked requires that the index is within the slice",
334            (end: usize = self.end(), len: usize = slice.len()) => end <= len
335        );
336        // SAFETY: the caller guarantees that `slice` is not dangling, so it
337        // cannot be longer than `isize::MAX`. They also guarantee that
338        // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
339        // so the call to `add` is safe.
340        unsafe { get_offset_len_noubcheck(slice, self.start(), self.len()) }
341    }
342
343    #[inline]
344    #[track_caller]
345    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
346        assert_unsafe_precondition!(
347            check_library_ub,
348            "slice::get_unchecked_mut requires that the index is within the slice",
349            (end: usize = self.end(), len: usize = slice.len()) => end <= len
350        );
351
352        // SAFETY: see comments for `get_unchecked` above.
353        unsafe { get_offset_len_mut_noubcheck(slice, self.start(), self.len()) }
354    }
355
356    #[inline]
357    fn index(self, slice: &[T]) -> &[T] {
358        if self.end() <= slice.len() {
359            // SAFETY: `self` is checked to be valid and in bounds above.
360            unsafe { &*get_offset_len_noubcheck(slice, self.start(), self.len()) }
361        } else {
362            slice_index_fail(self.start(), self.end(), slice.len())
363        }
364    }
365
366    #[inline]
367    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
368        if self.end() <= slice.len() {
369            // SAFETY: `self` is checked to be valid and in bounds above.
370            unsafe { &mut *get_offset_len_mut_noubcheck(slice, self.start(), self.len()) }
371        } else {
372            slice_index_fail(self.start(), self.end(), slice.len())
373        }
374    }
375}
376
377/// The methods `index` and `index_mut` panic if:
378/// - the start of the range is greater than the end of the range or
379/// - the end of the range is out of bounds.
380#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
381#[rustc_const_unstable(feature = "const_index", issue = "143775")]
382unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
383    type Output = [T];
384
385    #[inline]
386    fn get(self, slice: &[T]) -> Option<&[T]> {
387        // Using checked_sub is a safe way to get `SubUnchecked` in MIR
388        if let Some(new_len) = usize::checked_sub(self.end, self.start)
389            && self.end <= slice.len()
390        {
391            // SAFETY: `self` is checked to be valid and in bounds above.
392            unsafe { Some(&*get_offset_len_noubcheck(slice, self.start, new_len)) }
393        } else {
394            None
395        }
396    }
397
398    #[inline]
399    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
400        if let Some(new_len) = usize::checked_sub(self.end, self.start)
401            && self.end <= slice.len()
402        {
403            // SAFETY: `self` is checked to be valid and in bounds above.
404            unsafe { Some(&mut *get_offset_len_mut_noubcheck(slice, self.start, new_len)) }
405        } else {
406            None
407        }
408    }
409
410    #[inline]
411    #[track_caller]
412    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
413        assert_unsafe_precondition!(
414            check_library_ub,
415            "slice::get_unchecked requires that the range is within the slice",
416            (
417                start: usize = self.start,
418                end: usize = self.end,
419                len: usize = slice.len()
420            ) => end >= start && end <= len
421        );
422
423        // SAFETY: the caller guarantees that `slice` is not dangling, so it
424        // cannot be longer than `isize::MAX`. They also guarantee that
425        // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
426        // so the call to `add` is safe and the length calculation cannot overflow.
427        unsafe {
428            // Using the intrinsic avoids a superfluous UB check,
429            // since the one on this method already checked `end >= start`.
430            let new_len = crate::intrinsics::unchecked_sub(self.end, self.start);
431            get_offset_len_noubcheck(slice, self.start, new_len)
432        }
433    }
434
435    #[inline]
436    #[track_caller]
437    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
438        assert_unsafe_precondition!(
439            check_library_ub,
440            "slice::get_unchecked_mut requires that the range is within the slice",
441            (
442                start: usize = self.start,
443                end: usize = self.end,
444                len: usize = slice.len()
445            ) => end >= start && end <= len
446        );
447        // SAFETY: see comments for `get_unchecked` above.
448        unsafe {
449            let new_len = crate::intrinsics::unchecked_sub(self.end, self.start);
450            get_offset_len_mut_noubcheck(slice, self.start, new_len)
451        }
452    }
453
454    #[inline(always)]
455    fn index(self, slice: &[T]) -> &[T] {
456        // Using checked_sub is a safe way to get `SubUnchecked` in MIR
457        if let Some(new_len) = usize::checked_sub(self.end, self.start)
458            && self.end <= slice.len()
459        {
460            // SAFETY: `self` is checked to be valid and in bounds above.
461            unsafe { &*get_offset_len_noubcheck(slice, self.start, new_len) }
462        } else {
463            slice_index_fail(self.start, self.end, slice.len())
464        }
465    }
466
467    #[inline]
468    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
469        // Using checked_sub is a safe way to get `SubUnchecked` in MIR
470        if let Some(new_len) = usize::checked_sub(self.end, self.start)
471            && self.end <= slice.len()
472        {
473            // SAFETY: `self` is checked to be valid and in bounds above.
474            unsafe { &mut *get_offset_len_mut_noubcheck(slice, self.start, new_len) }
475        } else {
476            slice_index_fail(self.start, self.end, slice.len())
477        }
478    }
479}
480
481#[unstable(feature = "new_range_api", issue = "125687")]
482#[rustc_const_unstable(feature = "const_index", issue = "143775")]
483#[cfg(not(feature = "ferrocene_certified"))]
484unsafe impl<T> const SliceIndex<[T]> for range::Range<usize> {
485    type Output = [T];
486
487    #[inline]
488    fn get(self, slice: &[T]) -> Option<&[T]> {
489        ops::Range::from(self).get(slice)
490    }
491
492    #[inline]
493    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
494        ops::Range::from(self).get_mut(slice)
495    }
496
497    #[inline]
498    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
499        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
500        unsafe { ops::Range::from(self).get_unchecked(slice) }
501    }
502
503    #[inline]
504    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
505        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
506        unsafe { ops::Range::from(self).get_unchecked_mut(slice) }
507    }
508
509    #[inline(always)]
510    fn index(self, slice: &[T]) -> &[T] {
511        ops::Range::from(self).index(slice)
512    }
513
514    #[inline]
515    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
516        ops::Range::from(self).index_mut(slice)
517    }
518}
519
520/// The methods `index` and `index_mut` panic if the end of the range is out of bounds.
521#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
522#[rustc_const_unstable(feature = "const_index", issue = "143775")]
523unsafe impl<T> const SliceIndex<[T]> for ops::RangeTo<usize> {
524    type Output = [T];
525
526    #[inline]
527    fn get(self, slice: &[T]) -> Option<&[T]> {
528        (0..self.end).get(slice)
529    }
530
531    #[inline]
532    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
533        (0..self.end).get_mut(slice)
534    }
535
536    #[inline]
537    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
538        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
539        unsafe { (0..self.end).get_unchecked(slice) }
540    }
541
542    #[inline]
543    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
544        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
545        unsafe { (0..self.end).get_unchecked_mut(slice) }
546    }
547
548    #[inline(always)]
549    fn index(self, slice: &[T]) -> &[T] {
550        (0..self.end).index(slice)
551    }
552
553    #[inline]
554    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
555        (0..self.end).index_mut(slice)
556    }
557}
558
559/// The methods `index` and `index_mut` panic if the start of the range is out of bounds.
560#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
561#[rustc_const_unstable(feature = "const_index", issue = "143775")]
562unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
563    type Output = [T];
564
565    #[inline]
566    fn get(self, slice: &[T]) -> Option<&[T]> {
567        (self.start..slice.len()).get(slice)
568    }
569
570    #[inline]
571    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
572        (self.start..slice.len()).get_mut(slice)
573    }
574
575    #[inline]
576    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
577        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
578        unsafe { (self.start..slice.len()).get_unchecked(slice) }
579    }
580
581    #[inline]
582    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
583        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
584        unsafe { (self.start..slice.len()).get_unchecked_mut(slice) }
585    }
586
587    #[inline]
588    fn index(self, slice: &[T]) -> &[T] {
589        if self.start > slice.len() {
590            slice_index_fail(self.start, slice.len(), slice.len())
591        }
592        // SAFETY: `self` is checked to be valid and in bounds above.
593        unsafe {
594            let new_len = crate::intrinsics::unchecked_sub(slice.len(), self.start);
595            &*get_offset_len_noubcheck(slice, self.start, new_len)
596        }
597    }
598
599    #[inline]
600    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
601        if self.start > slice.len() {
602            slice_index_fail(self.start, slice.len(), slice.len())
603        }
604        // SAFETY: `self` is checked to be valid and in bounds above.
605        unsafe {
606            let new_len = crate::intrinsics::unchecked_sub(slice.len(), self.start);
607            &mut *get_offset_len_mut_noubcheck(slice, self.start, new_len)
608        }
609    }
610}
611
612#[unstable(feature = "new_range_api", issue = "125687")]
613#[rustc_const_unstable(feature = "const_index", issue = "143775")]
614#[cfg(not(feature = "ferrocene_certified"))]
615unsafe impl<T> const SliceIndex<[T]> for range::RangeFrom<usize> {
616    type Output = [T];
617
618    #[inline]
619    fn get(self, slice: &[T]) -> Option<&[T]> {
620        ops::RangeFrom::from(self).get(slice)
621    }
622
623    #[inline]
624    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
625        ops::RangeFrom::from(self).get_mut(slice)
626    }
627
628    #[inline]
629    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
630        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
631        unsafe { ops::RangeFrom::from(self).get_unchecked(slice) }
632    }
633
634    #[inline]
635    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
636        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
637        unsafe { ops::RangeFrom::from(self).get_unchecked_mut(slice) }
638    }
639
640    #[inline]
641    fn index(self, slice: &[T]) -> &[T] {
642        ops::RangeFrom::from(self).index(slice)
643    }
644
645    #[inline]
646    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
647        ops::RangeFrom::from(self).index_mut(slice)
648    }
649}
650
651#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
652#[rustc_const_unstable(feature = "const_index", issue = "143775")]
653unsafe impl<T> const SliceIndex<[T]> for ops::RangeFull {
654    type Output = [T];
655
656    #[inline]
657    fn get(self, slice: &[T]) -> Option<&[T]> {
658        Some(slice)
659    }
660
661    #[inline]
662    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
663        Some(slice)
664    }
665
666    #[inline]
667    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
668        slice
669    }
670
671    #[inline]
672    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
673        slice
674    }
675
676    #[inline]
677    fn index(self, slice: &[T]) -> &[T] {
678        slice
679    }
680
681    #[inline]
682    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
683        slice
684    }
685}
686
687/// The methods `index` and `index_mut` panic if:
688/// - the end of the range is `usize::MAX` or
689/// - the start of the range is greater than the end of the range or
690/// - the end of the range is out of bounds.
691#[stable(feature = "inclusive_range", since = "1.26.0")]
692#[rustc_const_unstable(feature = "const_index", issue = "143775")]
693unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
694    type Output = [T];
695
696    #[inline]
697    fn get(self, slice: &[T]) -> Option<&[T]> {
698        if *self.end() == usize::MAX { None } else { self.into_slice_range().get(slice) }
699    }
700
701    #[inline]
702    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
703        if *self.end() == usize::MAX { None } else { self.into_slice_range().get_mut(slice) }
704    }
705
706    #[inline]
707    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
708        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
709        unsafe { self.into_slice_range().get_unchecked(slice) }
710    }
711
712    #[inline]
713    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
714        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
715        unsafe { self.into_slice_range().get_unchecked_mut(slice) }
716    }
717
718    #[inline]
719    fn index(self, slice: &[T]) -> &[T] {
720        let Self { mut start, mut end, exhausted } = self;
721        let len = slice.len();
722        if end < len {
723            end = end + 1;
724            start = if exhausted { end } else { start };
725            if let Some(new_len) = usize::checked_sub(end, start) {
726                // SAFETY: `self` is checked to be valid and in bounds above.
727                unsafe { return &*get_offset_len_noubcheck(slice, start, new_len) }
728            }
729        }
730        slice_index_fail(start, end, slice.len())
731    }
732
733    #[inline]
734    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
735        let Self { mut start, mut end, exhausted } = self;
736        let len = slice.len();
737        if end < len {
738            end = end + 1;
739            start = if exhausted { end } else { start };
740            if let Some(new_len) = usize::checked_sub(end, start) {
741                // SAFETY: `self` is checked to be valid and in bounds above.
742                unsafe { return &mut *get_offset_len_mut_noubcheck(slice, start, new_len) }
743            }
744        }
745        slice_index_fail(start, end, slice.len())
746    }
747}
748
749#[unstable(feature = "new_range_api", issue = "125687")]
750#[rustc_const_unstable(feature = "const_index", issue = "143775")]
751#[cfg(not(feature = "ferrocene_certified"))]
752unsafe impl<T> const SliceIndex<[T]> for range::RangeInclusive<usize> {
753    type Output = [T];
754
755    #[inline]
756    fn get(self, slice: &[T]) -> Option<&[T]> {
757        ops::RangeInclusive::from(self).get(slice)
758    }
759
760    #[inline]
761    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
762        ops::RangeInclusive::from(self).get_mut(slice)
763    }
764
765    #[inline]
766    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
767        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
768        unsafe { ops::RangeInclusive::from(self).get_unchecked(slice) }
769    }
770
771    #[inline]
772    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
773        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
774        unsafe { ops::RangeInclusive::from(self).get_unchecked_mut(slice) }
775    }
776
777    #[inline]
778    fn index(self, slice: &[T]) -> &[T] {
779        ops::RangeInclusive::from(self).index(slice)
780    }
781
782    #[inline]
783    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
784        ops::RangeInclusive::from(self).index_mut(slice)
785    }
786}
787
788/// The methods `index` and `index_mut` panic if the end of the range is out of bounds.
789#[stable(feature = "inclusive_range", since = "1.26.0")]
790#[rustc_const_unstable(feature = "const_index", issue = "143775")]
791unsafe impl<T> const SliceIndex<[T]> for ops::RangeToInclusive<usize> {
792    type Output = [T];
793
794    #[inline]
795    fn get(self, slice: &[T]) -> Option<&[T]> {
796        (0..=self.end).get(slice)
797    }
798
799    #[inline]
800    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
801        (0..=self.end).get_mut(slice)
802    }
803
804    #[inline]
805    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
806        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
807        unsafe { (0..=self.end).get_unchecked(slice) }
808    }
809
810    #[inline]
811    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
812        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
813        unsafe { (0..=self.end).get_unchecked_mut(slice) }
814    }
815
816    #[inline]
817    fn index(self, slice: &[T]) -> &[T] {
818        (0..=self.end).index(slice)
819    }
820
821    #[inline]
822    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
823        (0..=self.end).index_mut(slice)
824    }
825}
826
827/// The methods `index` and `index_mut` panic if the end of the range is out of bounds.
828#[stable(feature = "inclusive_range", since = "1.26.0")]
829#[rustc_const_unstable(feature = "const_index", issue = "143775")]
830#[cfg(not(feature = "ferrocene_certified"))]
831unsafe impl<T> const SliceIndex<[T]> for range::RangeToInclusive<usize> {
832    type Output = [T];
833
834    #[inline]
835    fn get(self, slice: &[T]) -> Option<&[T]> {
836        (0..=self.last).get(slice)
837    }
838
839    #[inline]
840    fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
841        (0..=self.last).get_mut(slice)
842    }
843
844    #[inline]
845    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
846        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
847        unsafe { (0..=self.last).get_unchecked(slice) }
848    }
849
850    #[inline]
851    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
852        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
853        unsafe { (0..=self.last).get_unchecked_mut(slice) }
854    }
855
856    #[inline]
857    fn index(self, slice: &[T]) -> &[T] {
858        (0..=self.last).index(slice)
859    }
860
861    #[inline]
862    fn index_mut(self, slice: &mut [T]) -> &mut [T] {
863        (0..=self.last).index_mut(slice)
864    }
865}
866
867/// Performs bounds checking of a range.
868///
869/// This method is similar to [`Index::index`] for slices, but it returns a
870/// [`Range`] equivalent to `range`. You can use this method to turn any range
871/// into `start` and `end` values.
872///
873/// `bounds` is the range of the slice to use for bounds checking. It should
874/// be a [`RangeTo`] range that ends at the length of the slice.
875///
876/// The returned [`Range`] is safe to pass to [`slice::get_unchecked`] and
877/// [`slice::get_unchecked_mut`] for slices with the given range.
878///
879/// [`Range`]: ops::Range
880/// [`RangeTo`]: ops::RangeTo
881/// [`slice::get_unchecked`]: slice::get_unchecked
882/// [`slice::get_unchecked_mut`]: slice::get_unchecked_mut
883///
884/// # Panics
885///
886/// Panics if `range` would be out of bounds.
887///
888/// # Examples
889///
890/// ```
891/// #![feature(slice_range)]
892///
893/// use std::slice;
894///
895/// let v = [10, 40, 30];
896/// assert_eq!(1..2, slice::range(1..2, ..v.len()));
897/// assert_eq!(0..2, slice::range(..2, ..v.len()));
898/// assert_eq!(1..3, slice::range(1.., ..v.len()));
899/// ```
900///
901/// Panics when [`Index::index`] would panic:
902///
903/// ```should_panic
904/// #![feature(slice_range)]
905///
906/// use std::slice;
907///
908/// let _ = slice::range(2..1, ..3);
909/// ```
910///
911/// ```should_panic
912/// #![feature(slice_range)]
913///
914/// use std::slice;
915///
916/// let _ = slice::range(1..4, ..3);
917/// ```
918///
919/// ```should_panic
920/// #![feature(slice_range)]
921///
922/// use std::slice;
923///
924/// let _ = slice::range(1..=usize::MAX, ..3);
925/// ```
926///
927/// [`Index::index`]: ops::Index::index
928#[track_caller]
929#[unstable(feature = "slice_range", issue = "76393")]
930#[must_use]
931#[rustc_const_unstable(feature = "const_range", issue = "none")]
932#[cfg(not(feature = "ferrocene_certified"))]
933pub const fn range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
934where
935    R: [const] ops::RangeBounds<usize> + [const] Destruct,
936{
937    let len = bounds.end;
938
939    let end = match range.end_bound() {
940        ops::Bound::Included(&end) if end >= len => slice_index_fail(0, end, len),
941        // Cannot overflow because `end < len` implies `end < usize::MAX`.
942        ops::Bound::Included(&end) => end + 1,
943
944        ops::Bound::Excluded(&end) if end > len => slice_index_fail(0, end, len),
945        ops::Bound::Excluded(&end) => end,
946        ops::Bound::Unbounded => len,
947    };
948
949    let start = match range.start_bound() {
950        ops::Bound::Excluded(&start) if start >= end => slice_index_fail(start, end, len),
951        // Cannot overflow because `start < end` implies `start < usize::MAX`.
952        ops::Bound::Excluded(&start) => start + 1,
953
954        ops::Bound::Included(&start) if start > end => slice_index_fail(start, end, len),
955        ops::Bound::Included(&start) => start,
956
957        ops::Bound::Unbounded => 0,
958    };
959
960    ops::Range { start, end }
961}
962
963/// Performs bounds checking of a range without panicking.
964///
965/// This is a version of [`range()`] that returns [`None`] instead of panicking.
966///
967/// # Examples
968///
969/// ```
970/// #![feature(slice_range)]
971///
972/// use std::slice;
973///
974/// let v = [10, 40, 30];
975/// assert_eq!(Some(1..2), slice::try_range(1..2, ..v.len()));
976/// assert_eq!(Some(0..2), slice::try_range(..2, ..v.len()));
977/// assert_eq!(Some(1..3), slice::try_range(1.., ..v.len()));
978/// ```
979///
980/// Returns [`None`] when [`Index::index`] would panic:
981///
982/// ```
983/// #![feature(slice_range)]
984///
985/// use std::slice;
986///
987/// assert_eq!(None, slice::try_range(2..1, ..3));
988/// assert_eq!(None, slice::try_range(1..4, ..3));
989/// assert_eq!(None, slice::try_range(1..=usize::MAX, ..3));
990/// ```
991///
992/// [`Index::index`]: ops::Index::index
993#[unstable(feature = "slice_range", issue = "76393")]
994#[must_use]
995#[cfg(not(feature = "ferrocene_certified"))]
996pub fn try_range<R>(range: R, bounds: ops::RangeTo<usize>) -> Option<ops::Range<usize>>
997where
998    R: ops::RangeBounds<usize>,
999{
1000    let len = bounds.end;
1001
1002    let start = match range.start_bound() {
1003        ops::Bound::Included(&start) => start,
1004        ops::Bound::Excluded(start) => start.checked_add(1)?,
1005        ops::Bound::Unbounded => 0,
1006    };
1007
1008    let end = match range.end_bound() {
1009        ops::Bound::Included(end) => end.checked_add(1)?,
1010        ops::Bound::Excluded(&end) => end,
1011        ops::Bound::Unbounded => len,
1012    };
1013
1014    if start > end || end > len { None } else { Some(ops::Range { start, end }) }
1015}
1016
1017/// Converts a pair of `ops::Bound`s into `ops::Range` without performing any
1018/// bounds checking or (in debug) overflow checking.
1019pub(crate) const fn into_range_unchecked(
1020    len: usize,
1021    (start, end): (ops::Bound<usize>, ops::Bound<usize>),
1022) -> ops::Range<usize> {
1023    use ops::Bound;
1024    let start = match start {
1025        Bound::Included(i) => i,
1026        Bound::Excluded(i) => i + 1,
1027        Bound::Unbounded => 0,
1028    };
1029    let end = match end {
1030        Bound::Included(i) => i + 1,
1031        Bound::Excluded(i) => i,
1032        Bound::Unbounded => len,
1033    };
1034    start..end
1035}
1036
1037/// Converts pair of `ops::Bound`s into `ops::Range`.
1038/// Returns `None` on overflowing indices.
1039#[rustc_const_unstable(feature = "const_range", issue = "none")]
1040pub(crate) const fn into_range(
1041    len: usize,
1042    (start, end): (ops::Bound<usize>, ops::Bound<usize>),
1043) -> Option<ops::Range<usize>> {
1044    use ops::Bound;
1045    let start = match start {
1046        Bound::Included(start) => start,
1047        Bound::Excluded(start) => start.checked_add(1)?,
1048        Bound::Unbounded => 0,
1049    };
1050
1051    let end = match end {
1052        Bound::Included(end) => end.checked_add(1)?,
1053        Bound::Excluded(end) => end,
1054        Bound::Unbounded => len,
1055    };
1056
1057    // Don't bother with checking `start < end` and `end <= len`
1058    // since these checks are handled by `Range` impls
1059
1060    Some(start..end)
1061}
1062
1063/// Converts pair of `ops::Bound`s into `ops::Range`.
1064/// Panics on overflowing indices.
1065pub(crate) fn into_slice_range(
1066    len: usize,
1067    (start, end): (ops::Bound<usize>, ops::Bound<usize>),
1068) -> ops::Range<usize> {
1069    let end = match end {
1070        ops::Bound::Included(end) if end >= len => slice_index_fail(0, end, len),
1071        // Cannot overflow because `end < len` implies `end < usize::MAX`.
1072        ops::Bound::Included(end) => end + 1,
1073
1074        ops::Bound::Excluded(end) if end > len => slice_index_fail(0, end, len),
1075        ops::Bound::Excluded(end) => end,
1076
1077        ops::Bound::Unbounded => len,
1078    };
1079
1080    let start = match start {
1081        ops::Bound::Excluded(start) if start >= end => slice_index_fail(start, end, len),
1082        // Cannot overflow because `start < end` implies `start < usize::MAX`.
1083        ops::Bound::Excluded(start) => start + 1,
1084
1085        ops::Bound::Included(start) if start > end => slice_index_fail(start, end, len),
1086        ops::Bound::Included(start) => start,
1087
1088        ops::Bound::Unbounded => 0,
1089    };
1090
1091    start..end
1092}
1093
1094#[stable(feature = "slice_index_with_ops_bound_pair", since = "1.53.0")]
1095unsafe impl<T> SliceIndex<[T]> for (ops::Bound<usize>, ops::Bound<usize>) {
1096    type Output = [T];
1097
1098    #[inline]
1099    fn get(self, slice: &[T]) -> Option<&Self::Output> {
1100        into_range(slice.len(), self)?.get(slice)
1101    }
1102
1103    #[inline]
1104    fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output> {
1105        into_range(slice.len(), self)?.get_mut(slice)
1106    }
1107
1108    #[inline]
1109    unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output {
1110        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
1111        unsafe { into_range_unchecked(slice.len(), self).get_unchecked(slice) }
1112    }
1113
1114    #[inline]
1115    unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output {
1116        // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
1117        unsafe { into_range_unchecked(slice.len(), self).get_unchecked_mut(slice) }
1118    }
1119
1120    #[inline]
1121    fn index(self, slice: &[T]) -> &Self::Output {
1122        into_slice_range(slice.len(), self).index(slice)
1123    }
1124
1125    #[inline]
1126    fn index_mut(self, slice: &mut [T]) -> &mut Self::Output {
1127        into_slice_range(slice.len(), self).index_mut(slice)
1128    }
1129}