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