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