@@ -866,6 +866,7 @@ where
866
866
867
867
ops:: Bound :: Excluded ( & end) if end > len => slice_index_fail ( 0 , end, len) ,
868
868
ops:: Bound :: Excluded ( & end) => end,
869
+
869
870
ops:: Bound :: Unbounded => len,
870
871
} ;
871
872
@@ -921,19 +922,29 @@ where
921
922
{
922
923
let len = bounds. end ;
923
924
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
-
930
925
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 ,
932
931
ops:: Bound :: Excluded ( & end) => end,
932
+
933
933
ops:: Bound :: Unbounded => len,
934
934
} ;
935
935
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 } )
937
948
}
938
949
939
950
/// Converts a pair of `ops::Bound`s into `ops::Range` without performing any
@@ -962,21 +973,27 @@ pub(crate) fn into_range(
962
973
len : usize ,
963
974
( start, end) : ( ops:: Bound < usize > , ops:: Bound < usize > ) ,
964
975
) -> 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
-
972
976
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,
976
985
} ;
977
986
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
+ } ;
980
997
981
998
Some ( start..end)
982
999
}
0 commit comments