Auto merge of #119285 - lch361:split-no-panic, r=the8472

Removed redundant bounds checking at Split's next and next_back methods

Since these methods are using `Iterator::rposition`, which always returns a valid index, then there is no point in regular indexing with bounds checking
This commit is contained in:
bors 2023-12-26 06:24:22 +00:00
commit f6456285dd

View File

@ -458,8 +458,12 @@ fn next(&mut self) -> Option<&'a [T]> {
match self.v.iter().position(|x| (self.pred)(x)) {
None => self.finish(),
Some(idx) => {
let ret = Some(&self.v[..idx]);
self.v = &self.v[idx + 1..];
let (left, right) =
// SAFETY: if v.iter().position returns Some(idx), that
// idx is definitely a valid index for v
unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
let ret = Some(left);
self.v = right;
ret
}
}
@ -491,8 +495,12 @@ fn next_back(&mut self) -> Option<&'a [T]> {
match self.v.iter().rposition(|x| (self.pred)(x)) {
None => self.finish(),
Some(idx) => {
let ret = Some(&self.v[idx + 1..]);
self.v = &self.v[..idx];
let (left, right) =
// SAFETY: if v.iter().rposition returns Some(idx), then
// idx is definitely a valid index for v
unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
let ret = Some(right);
self.v = left;
ret
}
}