Skip to content

Commit af0783a

Browse files
committed
auto merge of #13241 : stepancheg/rust/push-all, r=alexcrichton
* push_all* operations should reserve capacity before pushing data to avoid unnecessary reallocations * reserve_exact should never shrink, as specified in documentation
2 parents e63346b + 026d206 commit af0783a

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/libstd/vec.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ impl<T: Clone> Vec<T> {
230230
/// ```
231231
#[inline]
232232
pub fn push_all(&mut self, other: &[T]) {
233-
for element in other.iter() {
234-
self.push((*element).clone())
235-
}
233+
self.extend(other.iter().map(|e| e.clone()));
236234
}
237235

238236
/// Grows the `Vec` in-place.
@@ -447,7 +445,7 @@ impl<T> Vec<T> {
447445
/// assert_eq!(vec.capacity(), 11);
448446
/// ```
449447
pub fn reserve_exact(&mut self, capacity: uint) {
450-
if capacity >= self.len {
448+
if capacity > self.cap {
451449
let size = capacity.checked_mul(&size_of::<T>()).expect("capacity overflow");
452450
self.cap = capacity;
453451
unsafe {
@@ -947,9 +945,7 @@ impl<T> Vec<T> {
947945
/// assert_eq!(vec, vec!(~1, ~2, ~3, ~4));
948946
/// ```
949947
pub fn push_all_move(&mut self, other: Vec<T>) {
950-
for element in other.move_iter() {
951-
self.push(element)
952-
}
948+
self.extend(other.move_iter());
953949
}
954950

955951
/// Returns a mutable slice of `self` between `start` and `end`.

0 commit comments

Comments
 (0)