Rearrange slice::split_mut to remove bounds check
This commit is contained in:
parent
9fb32dc924
commit
c9373903e7
1 changed files with 9 additions and 10 deletions
|
@ -710,18 +710,17 @@ where
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let idx_opt = {
|
match self.v.iter().position(|x| (self.pred)(x)) {
|
||||||
// work around borrowck limitations
|
|
||||||
let pred = &mut self.pred;
|
|
||||||
self.v.iter().position(|x| (*pred)(x))
|
|
||||||
};
|
|
||||||
match idx_opt {
|
|
||||||
None => self.finish(),
|
None => self.finish(),
|
||||||
Some(idx) => {
|
Some(idx) => {
|
||||||
let tmp = mem::replace(&mut self.v, &mut []);
|
let tmp = mem::take(&mut self.v);
|
||||||
let (head, tail) = tmp.split_at_mut(idx);
|
// idx is the index of the element we are splitting on. We want to set self to the
|
||||||
self.v = &mut tail[1..];
|
// region after idx, and return the subslice before and not including idx.
|
||||||
Some(head)
|
// 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])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue