@@ -866,6 +866,7 @@ where
866866
867867 ops:: Bound :: Excluded ( & end) if end > len => slice_index_fail ( 0 , end, len) ,
868868 ops:: Bound :: Excluded ( & end) => end,
869+
869870 ops:: Bound :: Unbounded => len,
870871 } ;
871872
@@ -921,19 +922,29 @@ where
921922{
922923 let len = bounds. end ;
923924
924- let start = match range. start_bound ( ) {
925- ops:: Bound :: Included ( & start) => start,
926- ops:: Bound :: Excluded ( start) => start. checked_add ( 1 ) ?,
927- ops:: Bound :: Unbounded => 0 ,
928- } ;
929-
930925 let end = match range. end_bound ( ) {
931- ops:: Bound :: Included ( end) => end. checked_add ( 1 ) ?,
926+ ops:: Bound :: Included ( & end) if end >= len => return None ,
927+ // Cannot overflow because `end < len` implies `end < usize::MAX`.
928+ ops:: Bound :: Included ( end) => end + 1 ,
929+
930+ ops:: Bound :: Excluded ( & end) if end > len => return None ,
932931 ops:: Bound :: Excluded ( & end) => end,
932+
933933 ops:: Bound :: Unbounded => len,
934934 } ;
935935
936- if start > end || end > len { None } else { Some ( ops:: Range { start, end } ) }
936+ let start = match range. start_bound ( ) {
937+ ops:: Bound :: Excluded ( & start) if start >= end => return None ,
938+ // Cannot overflow because `start < end` implies `start < usize::MAX`.
939+ ops:: Bound :: Excluded ( & start) => start + 1 ,
940+
941+ ops:: Bound :: Included ( & start) if start > end => return None ,
942+ ops:: Bound :: Included ( & start) => start,
943+
944+ ops:: Bound :: Unbounded => 0 ,
945+ } ;
946+
947+ Some ( ops:: Range { start, end } )
937948}
938949
939950/// Converts a pair of `ops::Bound`s into `ops::Range` without performing any
@@ -962,21 +973,27 @@ pub(crate) fn into_range(
962973 len : usize ,
963974 ( start, end) : ( ops:: Bound < usize > , ops:: Bound < usize > ) ,
964975) -> Option < ops:: Range < usize > > {
965- use ops:: Bound ;
966- let start = match start {
967- Bound :: Included ( start) => start,
968- Bound :: Excluded ( start) => start. checked_add ( 1 ) ?,
969- Bound :: Unbounded => 0 ,
970- } ;
971-
972976 let end = match end {
973- Bound :: Included ( end) => end. checked_add ( 1 ) ?,
974- Bound :: Excluded ( end) => end,
975- Bound :: Unbounded => len,
977+ ops:: Bound :: Included ( end) if end >= len => return None ,
978+ // Cannot overflow because `end < len` implies `end < usize::MAX`.
979+ ops:: Bound :: Included ( end) => end + 1 ,
980+
981+ ops:: Bound :: Excluded ( end) if end > len => return None ,
982+ ops:: Bound :: Excluded ( end) => end,
983+
984+ ops:: Bound :: Unbounded => len,
976985 } ;
977986
978- // Don't bother with checking `start < end` and `end <= len`
979- // since these checks are handled by `Range` impls
987+ let start = match start {
988+ ops:: Bound :: Excluded ( start) if start >= end => return None ,
989+ // Cannot overflow because `start < end` implies `start < usize::MAX`.
990+ ops:: Bound :: Excluded ( start) => start + 1 ,
991+
992+ ops:: Bound :: Included ( start) if start > end => return None ,
993+ ops:: Bound :: Included ( start) => start,
994+
995+ ops:: Bound :: Unbounded => 0 ,
996+ } ;
980997
981998 Some ( start..end)
982999}
0 commit comments