Skip to content

Commit 7e81c11

Browse files
committed
tweak swap_remove
1 parent 5bbaac3 commit 7e81c11

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/liballoc/vec.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -963,14 +963,15 @@ impl<T> Vec<T> {
963963
#[inline]
964964
#[stable(feature = "rust1", since = "1.0.0")]
965965
pub fn swap_remove(&mut self, index: usize) -> T {
966-
assert!(index < self.len);
966+
let len = self.len();
967+
assert!(index < len);
967968
unsafe {
968969
// We replace self[index] with the last element. Note that if the
969970
// bounds check above succeeds there must be a last element (which
970971
// can be self[index] itself).
971-
let last = ptr::read(self.as_ptr().add(self.len - 1));
972+
let last = ptr::read(self.as_ptr().add(len - 1));
972973
let hole: *mut T = self.as_mut_ptr().add(index);
973-
self.len -= 1;
974+
self.set_len(len - 1);
974975
ptr::replace(hole, last)
975976
}
976977
}

0 commit comments

Comments
 (0)