1use 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#[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 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#[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 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 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 #[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#[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] #[rustc_const_unstable(feature = "const_index", issue = "143775")]
183pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
184 #[stable(feature = "slice_get_slice", since = "1.28.0")]
186 type Output: ?Sized;
187
188 #[unstable(feature = "slice_index_methods", issue = "none")]
191 fn get(self, slice: &T) -> Option<&Self::Output>;
192
193 #[unstable(feature = "slice_index_methods", issue = "none")]
196 fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
197
198 #[unstable(feature = "slice_index_methods", issue = "none")]
206 unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
207
208 #[unstable(feature = "slice_index_methods", issue = "none")]
216 unsafe fn get_unchecked_mut(self, slice: *mut T) -> *mut Self::Output;
217
218 #[unstable(feature = "slice_index_methods", issue = "none")]
221 #[track_caller]
222 fn index(self, slice: &T) -> &Self::Output;
223
224 #[unstable(feature = "slice_index_methods", issue = "none")]
227 #[track_caller]
228 fn index_mut(self, slice: &mut T) -> &mut Self::Output;
229}
230
231#[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 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 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, "slice::get_unchecked requires that the index is within the slice",
263 (this: usize = self, len: usize = slice.len()) => this < len
264 );
265 unsafe {
270 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 unsafe { slice_get_unchecked(slice, self) }
287 }
288
289 #[inline]
290 fn index(self, slice: &[T]) -> &T {
291 &(*slice)[self]
293 }
294
295 #[inline]
296 fn index_mut(self, slice: &mut [T]) -> &mut T {
297 &mut (*slice)[self]
299 }
300}
301
302#[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 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 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 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 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 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 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#[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 if let Some(new_len) = usize::checked_sub(self.end, self.start)
389 && self.end <= slice.len()
390 {
391 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 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 unsafe {
428 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 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 if let Some(new_len) = usize::checked_sub(self.end, self.start)
458 && self.end <= slice.len()
459 {
460 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 if let Some(new_len) = usize::checked_sub(self.end, self.start)
471 && self.end <= slice.len()
472 {
473 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 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 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#[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 unsafe { (0..self.end).get_unchecked(slice) }
540 }
541
542 #[inline]
543 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
544 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#[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 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 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 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 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 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 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#[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 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 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 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 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 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 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#[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 unsafe { (0..=self.end).get_unchecked(slice) }
808 }
809
810 #[inline]
811 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
812 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#[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 unsafe { (0..=self.last).get_unchecked(slice) }
848 }
849
850 #[inline]
851 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
852 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#[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 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 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#[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
1017pub(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#[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 Some(start..end)
1061}
1062
1063pub(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 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 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 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 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}