From 08e95a87b84aad8051ea806c963d089effaf0a7f Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Tue, 1 Apr 2014 20:16:59 +0000 Subject: [PATCH 1/2] Vec::reserve_exact should not shrink reserve_exact should not shrink according to documentation. --- src/libstd/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 5e42aaecbb93c..4dd1b5d08c0b9 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -447,7 +447,7 @@ impl Vec { /// 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::()).expect("capacity overflow"); self.cap = capacity; unsafe { From 026d206aa1e23a2496b5de47c0caf99024cc0f27 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Tue, 1 Apr 2014 20:16:59 +0000 Subject: [PATCH 2/2] Reimplement Vec::push_all* with .extend It is shorter and also fixes missed reserve call. --- src/libstd/vec.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 4dd1b5d08c0b9..69c3a85b2f160 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -230,9 +230,7 @@ impl Vec { /// ``` #[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. @@ -947,9 +945,7 @@ impl Vec { /// assert_eq!(vec, vec!(~1, ~2, ~3, ~4)); /// ``` pub fn push_all_move(&mut self, other: Vec) { - for element in other.move_iter() { - self.push(element) - } + self.extend(other.move_iter()); } /// Returns a mutable slice of `self` between `start` and `end`.