Skip to content

Commit ba32940

Browse files
committed
Auto merge of #151726 - scottmcm:delete-duplicated-code, r=<try>
Remove duplicated code in `slice/index.rs`
2 parents 36e2b8a + a3f169c commit ba32940

File tree

2 files changed

+7
-39
lines changed

2 files changed

+7
-39
lines changed

library/core/src/ops/range.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ impl<T: Copy> Bound<&T> {
773773
/// ```
774774
#[unstable(feature = "bound_copied", issue = "145966")]
775775
#[must_use]
776-
pub fn copied(self) -> Bound<T> {
776+
pub const fn copied(self) -> Bound<T> {
777777
match self {
778778
Bound::Unbounded => Bound::Unbounded,
779779
Bound::Included(x) => Bound::Included(*x),

library/core/src/slice/index.rs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -910,29 +910,7 @@ where
910910
R: [const] ops::RangeBounds<usize> + [const] Destruct,
911911
{
912912
let len = bounds.end;
913-
914-
let end = match range.end_bound() {
915-
ops::Bound::Included(&end) if end >= len => slice_index_fail(0, end, len),
916-
// Cannot overflow because `end < len` implies `end < usize::MAX`.
917-
ops::Bound::Included(&end) => end + 1,
918-
919-
ops::Bound::Excluded(&end) if end > len => slice_index_fail(0, end, len),
920-
ops::Bound::Excluded(&end) => end,
921-
ops::Bound::Unbounded => len,
922-
};
923-
924-
let start = match range.start_bound() {
925-
ops::Bound::Excluded(&start) if start >= end => slice_index_fail(start, end, len),
926-
// Cannot overflow because `start < end` implies `start < usize::MAX`.
927-
ops::Bound::Excluded(&start) => start + 1,
928-
929-
ops::Bound::Included(&start) if start > end => slice_index_fail(start, end, len),
930-
ops::Bound::Included(&start) => start,
931-
932-
ops::Bound::Unbounded => 0,
933-
};
934-
935-
ops::Range { start, end }
913+
into_slice_range(len, (range.start_bound().copied(), range.end_bound().copied()))
936914
}
937915

938916
/// Performs bounds checking of a range without panicking.
@@ -972,20 +950,8 @@ where
972950
R: ops::RangeBounds<usize>,
973951
{
974952
let len = bounds.end;
975-
976-
let start = match range.start_bound() {
977-
ops::Bound::Included(&start) => start,
978-
ops::Bound::Excluded(start) => start.checked_add(1)?,
979-
ops::Bound::Unbounded => 0,
980-
};
981-
982-
let end = match range.end_bound() {
983-
ops::Bound::Included(end) => end.checked_add(1)?,
984-
ops::Bound::Excluded(&end) => end,
985-
ops::Bound::Unbounded => len,
986-
};
987-
988-
if start > end || end > len { None } else { Some(ops::Range { start, end }) }
953+
let r = into_range(len, (range.start_bound().copied(), range.end_bound().copied()))?;
954+
if r.start > r.end || r.end > len { None } else { Some(r) }
989955
}
990956

991957
/// Converts a pair of `ops::Bound`s into `ops::Range` without performing any
@@ -1011,6 +977,7 @@ pub(crate) const fn into_range_unchecked(
1011977
/// Converts pair of `ops::Bound`s into `ops::Range`.
1012978
/// Returns `None` on overflowing indices.
1013979
#[rustc_const_unstable(feature = "const_range", issue = "none")]
980+
#[inline]
1014981
pub(crate) const fn into_range(
1015982
len: usize,
1016983
(start, end): (ops::Bound<usize>, ops::Bound<usize>),
@@ -1036,7 +1003,8 @@ pub(crate) const fn into_range(
10361003

10371004
/// Converts pair of `ops::Bound`s into `ops::Range`.
10381005
/// Panics on overflowing indices.
1039-
pub(crate) fn into_slice_range(
1006+
#[inline]
1007+
pub(crate) const fn into_slice_range(
10401008
len: usize,
10411009
(start, end): (ops::Bound<usize>, ops::Bound<usize>),
10421010
) -> ops::Range<usize> {

0 commit comments

Comments
 (0)