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
This commit is contained in:
bors 2014-04-01 21:21:48 -07:00
commit af0783aa1f

View File

@ -230,9 +230,7 @@ impl<T: Clone> Vec<T> {
/// ``` /// ```
#[inline] #[inline]
pub fn push_all(&mut self, other: &[T]) { pub fn push_all(&mut self, other: &[T]) {
for element in other.iter() { self.extend(other.iter().map(|e| e.clone()));
self.push((*element).clone())
}
} }
/// Grows the `Vec` in-place. /// Grows the `Vec` in-place.
@ -447,7 +445,7 @@ impl<T> Vec<T> {
/// assert_eq!(vec.capacity(), 11); /// assert_eq!(vec.capacity(), 11);
/// ``` /// ```
pub fn reserve_exact(&mut self, capacity: uint) { pub fn reserve_exact(&mut self, capacity: uint) {
if capacity >= self.len { if capacity > self.cap {
let size = capacity.checked_mul(&size_of::<T>()).expect("capacity overflow"); let size = capacity.checked_mul(&size_of::<T>()).expect("capacity overflow");
self.cap = capacity; self.cap = capacity;
unsafe { unsafe {
@ -947,9 +945,7 @@ impl<T> Vec<T> {
/// assert_eq!(vec, vec!(~1, ~2, ~3, ~4)); /// assert_eq!(vec, vec!(~1, ~2, ~3, ~4));
/// ``` /// ```
pub fn push_all_move(&mut self, other: Vec<T>) { pub fn push_all_move(&mut self, other: Vec<T>) {
for element in other.move_iter() { self.extend(other.move_iter());
self.push(element)
}
} }
/// Returns a mutable slice of `self` between `start` and `end`. /// Returns a mutable slice of `self` between `start` and `end`.