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#[rustc_const_unstable(feature = "const_index", issue = "143775")]
182pub const unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
183 #[stable(feature = "slice_get_slice", since = "1.28.0")]
185 type Output: ?Sized;
186
187 #[unstable(feature = "slice_index_methods", issue = "none")]
190 fn get(self, slice: &T) -> Option<&Self::Output>;
191
192 #[unstable(feature = "slice_index_methods", issue = "none")]
195 fn get_mut(self, slice: &mut T) -> Option<&mut Self::Output>;
196
197 #[unstable(feature = "slice_index_methods", issue = "none")]
205 unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
206
207 #[unstable(feature = "slice_index_methods", issue = "none")]
215 unsafe fn get_unchecked_mut(self, slice: *mut T) -> *mut Self::Output;
216
217 #[unstable(feature = "slice_index_methods", issue = "none")]
220 #[track_caller]
221 fn index(self, slice: &T) -> &Self::Output;
222
223 #[unstable(feature = "slice_index_methods", issue = "none")]
226 #[track_caller]
227 fn index_mut(self, slice: &mut T) -> &mut Self::Output;
228}
229
230#[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 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 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, "slice::get_unchecked requires that the index is within the slice",
262 (this: usize = self, len: usize = slice.len()) => this < len
263 );
264 unsafe {
269 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 unsafe { slice_get_unchecked(slice, self) }
286 }
287
288 #[inline]
289 fn index(self, slice: &[T]) -> &T {
290 &(*slice)[self]
292 }
293
294 #[inline]
295 fn index_mut(self, slice: &mut [T]) -> &mut T {
296 &mut (*slice)[self]
298 }
299}
300
301#[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 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 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 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 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 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 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#[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 if let Some(new_len) = usize::checked_sub(self.end, self.start)
388 && self.end <= slice.len()
389 {
390 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 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 unsafe {
427 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 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 if let Some(new_len) = usize::checked_sub(self.end, self.start)
457 && self.end <= slice.len()
458 {
459 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 if let Some(new_len) = usize::checked_sub(self.end, self.start)
470 && self.end <= slice.len()
471 {
472 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 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 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#[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 unsafe { (0..self.end).get_unchecked(slice) }
539 }
540
541 #[inline]
542 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
543 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#[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 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 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 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 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 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 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#[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 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 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 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 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 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 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#[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 unsafe { (0..=self.end).get_unchecked(slice) }
807 }
808
809 #[inline]
810 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
811 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#[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 unsafe { (0..=self.last).get_unchecked(slice) }
847 }
848
849 #[inline]
850 unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
851 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#[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 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 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#[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
1016pub(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#[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 Some(start..end)
1060}
1061
1062pub(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 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 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 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 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}