diff options
| author | bors <bors@rust-lang.org> | 2014-04-01 21:21:48 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-04-01 21:21:48 -0700 |
| commit | af0783aa1f26834adbbcb6f9c84451fd5b929553 (patch) | |
| tree | 85ff0e501c5d017c2c0ea66ff935641b804cb6b4 | |
| parent | e63346b9d8b1a8f38d9a3dbd694cc77b08ffa1e7 (diff) | |
| parent | 026d206aa1e23a2496b5de47c0caf99024cc0f27 (diff) | |
| download | rust-af0783aa1f26834adbbcb6f9c84451fd5b929553.tar.gz rust-af0783aa1f26834adbbcb6f9c84451fd5b929553.zip | |
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
| -rw-r--r-- | src/libstd/vec.rs | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 5e42aaecbb9..69c3a85b2f1 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -230,9 +230,7 @@ impl<T: Clone> Vec<T> { /// ``` #[inline] pub fn push_all(&mut self, other: &[T]) { - for element in other.iter() { - self.push((*element).clone()) - } + self.extend(other.iter().map(|e| e.clone())); } /// Grows the `Vec` in-place. @@ -447,7 +445,7 @@ impl<T> Vec<T> { /// assert_eq!(vec.capacity(), 11); /// ``` 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"); self.cap = capacity; unsafe { @@ -947,9 +945,7 @@ impl<T> Vec<T> { /// assert_eq!(vec, vec!(~1, ~2, ~3, ~4)); /// ``` pub fn push_all_move(&mut self, other: Vec<T>) { - for element in other.move_iter() { - self.push(element) - } + self.extend(other.move_iter()); } /// Returns a mutable slice of `self` between `start` and `end`. |
