@@ -907,6 +907,7 @@ where
907
907
908
908
ops:: Bound :: Excluded ( & end) if end > len => slice_index_fail ( 0 , end, len) ,
909
909
ops:: Bound :: Excluded ( & end) => end,
910
+
910
911
ops:: Bound :: Unbounded => len,
911
912
} ;
912
913
@@ -962,19 +963,29 @@ where
962
963
{
963
964
let len = bounds. end ;
964
965
965
- let start = match range. start_bound ( ) {
966
- ops:: Bound :: Included ( & start) => start,
967
- ops:: Bound :: Excluded ( start) => start. checked_add ( 1 ) ?,
968
- ops:: Bound :: Unbounded => 0 ,
969
- } ;
970
-
971
966
let end = match range. end_bound ( ) {
972
- ops:: Bound :: Included ( end) => end. checked_add ( 1 ) ?,
967
+ ops:: Bound :: Included ( & end) if end >= len => return None ,
968
+ // Cannot overflow because `end < len` implies `end < usize::MAX`.
969
+ ops:: Bound :: Included ( end) => end + 1 ,
970
+
971
+ ops:: Bound :: Excluded ( & end) if end > len => return None ,
973
972
ops:: Bound :: Excluded ( & end) => end,
973
+
974
974
ops:: Bound :: Unbounded => len,
975
975
} ;
976
976
977
- if start > end || end > len { None } else { Some ( ops:: Range { start, end } ) }
977
+ let start = match range. start_bound ( ) {
978
+ ops:: Bound :: Excluded ( & start) if start >= end => return None ,
979
+ // Cannot overflow because `start < end` implies `start < usize::MAX`.
980
+ ops:: Bound :: Excluded ( & start) => start + 1 ,
981
+
982
+ ops:: Bound :: Included ( & start) if start > end => return None ,
983
+ ops:: Bound :: Included ( & start) => start,
984
+
985
+ ops:: Bound :: Unbounded => 0 ,
986
+ } ;
987
+
988
+ Some ( ops:: Range { start, end } )
978
989
}
979
990
980
991
/// Converts a pair of `ops::Bound`s into `ops::Range` without performing any
@@ -1003,21 +1014,27 @@ pub(crate) fn into_range(
1003
1014
len : usize ,
1004
1015
( start, end) : ( ops:: Bound < usize > , ops:: Bound < usize > ) ,
1005
1016
) -> Option < ops:: Range < usize > > {
1006
- use ops:: Bound ;
1007
- let start = match start {
1008
- Bound :: Included ( start) => start,
1009
- Bound :: Excluded ( start) => start. checked_add ( 1 ) ?,
1010
- Bound :: Unbounded => 0 ,
1011
- } ;
1012
-
1013
1017
let end = match end {
1014
- Bound :: Included ( end) => end. checked_add ( 1 ) ?,
1015
- Bound :: Excluded ( end) => end,
1016
- Bound :: Unbounded => len,
1018
+ ops:: Bound :: Included ( end) if end >= len => return None ,
1019
+ // Cannot overflow because `end < len` implies `end < usize::MAX`.
1020
+ ops:: Bound :: Included ( end) => end + 1 ,
1021
+
1022
+ ops:: Bound :: Excluded ( end) if end > len => return None ,
1023
+ ops:: Bound :: Excluded ( end) => end,
1024
+
1025
+ ops:: Bound :: Unbounded => len,
1017
1026
} ;
1018
1027
1019
- // Don't bother with checking `start < end` and `end <= len`
1020
- // since these checks are handled by `Range` impls
1028
+ let start = match start {
1029
+ ops:: Bound :: Excluded ( start) if start >= end => return None ,
1030
+ // Cannot overflow because `start < end` implies `start < usize::MAX`.
1031
+ ops:: Bound :: Excluded ( start) => start + 1 ,
1032
+
1033
+ ops:: Bound :: Included ( start) if start > end => return None ,
1034
+ ops:: Bound :: Included ( start) => start,
1035
+
1036
+ ops:: Bound :: Unbounded => 0 ,
1037
+ } ;
1021
1038
1022
1039
Some ( start..end)
1023
1040
}
0 commit comments