Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rearrange slice::split_mut to remove bounds check #99223

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,18 +710,17 @@ where
return None;
}

let idx_opt = {
// work around borrowck limitations
let pred = &mut self.pred;
self.v.iter().position(|x| (*pred)(x))
};
match idx_opt {
match self.v.iter().position(|x| (self.pred)(x)) {
None => self.finish(),
Some(idx) => {
let tmp = mem::replace(&mut self.v, &mut []);
let (head, tail) = tmp.split_at_mut(idx);
self.v = &mut tail[1..];
Some(head)
let tmp = mem::take(&mut self.v);
// idx is the index of the element we are splitting on. We want to set self to the
// region after idx, and return the subslice before and not including idx.
// So first we split after idx
let (head, tail) = tmp.split_at_mut(idx + 1);
self.v = tail;
// Then return the subslice up to but not including the found element
Some(&mut head[..idx])
}
}
}
Expand Down