Skip to content

Commit 13242ef

Browse files
Jules BertholetJules-Bertholet
authored andcommitted
Return empty slice on empty slice input (match #89716)
1 parent 176be5f commit 13242ef

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

library/alloc/tests/slice.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ fn test_splitator_left_inclusive() {
943943
assert_eq!(xs.split_left_inclusive(|_| true).collect::<Vec<&[i32]>>(), splits);
944944

945945
let xs: &[i32] = &[];
946-
let splits: &[&[i32]] = &[&[]];
946+
let splits: &[&[i32]] = &[];
947947
assert_eq!(xs.split_left_inclusive(|x| *x == 5).collect::<Vec<&[i32]>>(), splits);
948948
}
949949

@@ -963,7 +963,7 @@ fn test_splitator_left_inclusive_reverse() {
963963
assert_eq!(xs.split_left_inclusive(|_| true).rev().collect::<Vec<_>>(), splits);
964964

965965
let xs: &[i32] = &[];
966-
let splits: &[&[i32]] = &[&[]];
966+
let splits: &[&[i32]] = &[];
967967
assert_eq!(xs.split_left_inclusive(|x| *x == 5).rev().collect::<Vec<_>>(), splits);
968968
}
969969

@@ -983,7 +983,7 @@ fn test_splitator_left_inclusive_mut() {
983983
assert_eq!(xs.split_left_inclusive_mut(|_| true).collect::<Vec<_>>(), splits);
984984

985985
let xs: &mut [i32] = &mut [];
986-
let splits: &[&[i32]] = &[&[]];
986+
let splits: &[&[i32]] = &[];
987987
assert_eq!(xs.split_left_inclusive_mut(|x| *x == 5).collect::<Vec<_>>(), splits);
988988
}
989989

@@ -1003,7 +1003,7 @@ fn test_splitator_left_inclusive_reverse_mut() {
10031003
assert_eq!(xs.split_left_inclusive_mut(|_| true).rev().collect::<Vec<_>>(), splits);
10041004

10051005
let xs: &mut [i32] = &mut [];
1006-
let splits: &[&[i32]] = &[&[]];
1006+
let splits: &[&[i32]] = &[];
10071007
assert_eq!(xs.split_left_inclusive_mut(|x| *x == 5).rev().collect::<Vec<_>>(), splits);
10081008
}
10091009

library/core/src/slice/iter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,8 @@ where
670670
impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitLeftInclusive<'a, T, P> {
671671
#[inline]
672672
pub(super) fn new(slice: &'a [T], pred: P) -> Self {
673-
Self { v: slice, pred, finished: false }
673+
let finished = slice.is_empty();
674+
Self { v: slice, pred, finished }
674675
}
675676
}
676677

@@ -1048,7 +1049,8 @@ where
10481049
impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitLeftInclusiveMut<'a, T, P> {
10491050
#[inline]
10501051
pub(super) fn new(slice: &'a mut [T], pred: P) -> Self {
1051-
Self { v: slice, pred, finished: false }
1052+
let finished = slice.is_empty();
1053+
Self { v: slice, pred, finished }
10521054
}
10531055
}
10541056

0 commit comments

Comments
 (0)