From b4984a490b7fe181ec72b8fb0188bbd0ab4c15e0 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 7 Jul 2014 09:45:00 -0700 Subject: [PATCH] collections: merge unsafe_push_all_clone and push_all --- src/libcollections/vec.rs | 43 +++++++++++++-------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 3c38e6d1c2e8..03d06253a5ec 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -197,14 +197,8 @@ impl Vec { /// ``` #[inline] pub fn from_slice(values: &[T]) -> Vec { - let mut vector = Vec::with_capacity(values.len()); - - // Directly call `unsafe_push_all_clone` so we can skip a call to - // `reserve_addtional`. - unsafe { - unsafe_push_all_clone(&mut vector, values); - } - + let mut vector = Vec::new(); + vector.push_all(values); vector } @@ -248,8 +242,18 @@ impl Vec { pub fn push_all(&mut self, other: &[T]) { self.reserve_additional(other.len()); - unsafe { - unsafe_push_all_clone(self, other) + for i in range(0, other.len()) { + let len = self.len(); + + // Unsafe code so this can be optimised to a memcpy (or something similarly + // fast) when T is Copy. LLVM is easily confused, so any extra operations + // during the loop can prevent this optimisation. + unsafe { + ptr::write( + self.as_mut_slice().unsafe_mut_ref(len), + other.unsafe_ref(i).clone()); + self.set_len(len + 1); + } } } @@ -1550,25 +1554,6 @@ pub mod raw { } } -// Unsafe code so this can be optimised to a memcpy (or something similarly -// fast) when T is Copy. LLVM is easily confused, so any extra operations -// during the loop can prevent this optimisation. -// -// WARNING: You must preallocate space on the vector before you call this -// method. -#[inline(always)] -unsafe fn unsafe_push_all_clone(dst: &mut Vec, src: &[T]) { - let mut dst_len = dst.len(); - - for i in range(0, src.len()) { - ptr::write( - dst.as_mut_slice().unsafe_mut_ref(dst_len), - src.unsafe_ref(i).clone()); - dst_len += 1; - dst.set_len(dst_len); - } -} - #[cfg(test)] mod tests { extern crate test;