mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
tweak swap_remove
This commit is contained in:
parent
5bbaac357d
commit
7e81c11aa8
@ -963,14 +963,15 @@ impl<T> Vec<T> {
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn swap_remove(&mut self, index: usize) -> T {
|
||||
assert!(index < self.len);
|
||||
let len = self.len();
|
||||
assert!(index < len);
|
||||
unsafe {
|
||||
// We replace self[index] with the last element. Note that if the
|
||||
// bounds check above succeeds there must be a last element (which
|
||||
// can be self[index] itself).
|
||||
let last = ptr::read(self.as_ptr().add(self.len - 1));
|
||||
let last = ptr::read(self.as_ptr().add(len - 1));
|
||||
let hole: *mut T = self.as_mut_ptr().add(index);
|
||||
self.len -= 1;
|
||||
self.set_len(len - 1);
|
||||
ptr::replace(hole, last)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user