@@ -12,7 +12,7 @@ use crate::cmp::Ordering::{self, Equal, Greater, Less};
1212use crate :: intrinsics:: assume;
1313use crate :: marker:: { self , Copy } ;
1414use crate :: mem;
15- use crate :: ops:: { Bound , FnMut , Range , RangeBounds } ;
15+ use crate :: ops:: { FnMut , Range , RangeBounds } ;
1616use crate :: option:: Option ;
1717use crate :: option:: Option :: { None , Some } ;
1818use crate :: ptr:: { self , NonNull } ;
@@ -72,8 +72,8 @@ pub use sort::heapsort;
7272#[ stable( feature = "slice_get_slice" , since = "1.28.0" ) ]
7373pub use index:: SliceIndex ;
7474
75- use index :: { slice_end_index_len_fail , slice_index_order_fail } ;
76- use index:: { slice_end_index_overflow_fail , slice_start_index_overflow_fail } ;
75+ # [ unstable ( feature = "slice_check_range" , issue = "76393" ) ]
76+ pub use index:: check_range ;
7777
7878#[ lang = "slice" ]
7979#[ cfg( not( test) ) ]
@@ -378,79 +378,6 @@ impl<T> [T] {
378378 unsafe { & mut * index. get_unchecked_mut ( self ) }
379379 }
380380
381- /// Converts a range over this slice to [`Range`].
382- ///
383- /// The returned range is safe to pass to [`get_unchecked`] and [`get_unchecked_mut`].
384- ///
385- /// [`get_unchecked`]: #method.get_unchecked
386- /// [`get_unchecked_mut`]: #method.get_unchecked_mut
387- ///
388- /// # Panics
389- ///
390- /// Panics if the range is out of bounds.
391- ///
392- /// # Examples
393- ///
394- /// ```
395- /// #![feature(slice_check_range)]
396- ///
397- /// let v = [10, 40, 30];
398- /// assert_eq!(1..2, v.check_range(1..2));
399- /// assert_eq!(0..2, v.check_range(..2));
400- /// assert_eq!(1..3, v.check_range(1..));
401- /// ```
402- ///
403- /// Panics when [`Index::index`] would panic:
404- ///
405- /// ```should_panic
406- /// #![feature(slice_check_range)]
407- ///
408- /// [10, 40, 30].check_range(2..1);
409- /// ```
410- ///
411- /// ```should_panic
412- /// #![feature(slice_check_range)]
413- ///
414- /// [10, 40, 30].check_range(1..4);
415- /// ```
416- ///
417- /// ```should_panic
418- /// #![feature(slice_check_range)]
419- ///
420- /// [10, 40, 30].check_range(1..=usize::MAX);
421- /// ```
422- ///
423- /// [`Index::index`]: crate::ops::Index::index
424- #[ track_caller]
425- #[ unstable( feature = "slice_check_range" , issue = "76393" ) ]
426- pub fn check_range < R : RangeBounds < usize > > ( & self , range : R ) -> Range < usize > {
427- let start = match range. start_bound ( ) {
428- Bound :: Included ( & start) => start,
429- Bound :: Excluded ( start) => {
430- start. checked_add ( 1 ) . unwrap_or_else ( || slice_start_index_overflow_fail ( ) )
431- }
432- Bound :: Unbounded => 0 ,
433- } ;
434-
435- let len = self . len ( ) ;
436- let end = match range. end_bound ( ) {
437- Bound :: Included ( end) => {
438- end. checked_add ( 1 ) . unwrap_or_else ( || slice_end_index_overflow_fail ( ) )
439- }
440- Bound :: Excluded ( & end) => end,
441- Bound :: Unbounded => len,
442- } ;
443-
444- if start > end {
445- slice_index_order_fail ( start, end) ;
446- }
447- if end > len {
448- slice_end_index_len_fail ( end, len) ;
449- }
450-
451- Range { start, end }
452- }
453-
454381 /// Returns a raw pointer to the slice's buffer.
455382 ///
456383 /// The caller must ensure that the slice outlives the pointer this
@@ -2794,7 +2721,7 @@ impl<T> [T] {
27942721 where
27952722 T : Copy ,
27962723 {
2797- let Range { start : src_start, end : src_end } = self . check_range ( src) ;
2724+ let Range { start : src_start, end : src_end } = check_range ( self . len ( ) , src) ;
27982725 let count = src_end - src_start;
27992726 assert ! ( dest <= self . len( ) - count, "dest is out of bounds" ) ;
28002727 // SAFETY: the conditions for `ptr::copy` have all been checked above,
0 commit comments