Skip to content

Commit b4984a4

Browse files
committed
collections: merge unsafe_push_all_clone and push_all
1 parent e2d107c commit b4984a4

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

src/libcollections/vec.rs

+14-29
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,8 @@ impl<T: Clone> Vec<T> {
197197
/// ```
198198
#[inline]
199199
pub fn from_slice(values: &[T]) -> Vec<T> {
200-
let mut vector = Vec::with_capacity(values.len());
201-
202-
// Directly call `unsafe_push_all_clone` so we can skip a call to
203-
// `reserve_addtional`.
204-
unsafe {
205-
unsafe_push_all_clone(&mut vector, values);
206-
}
207-
200+
let mut vector = Vec::new();
201+
vector.push_all(values);
208202
vector
209203
}
210204

@@ -248,8 +242,18 @@ impl<T: Clone> Vec<T> {
248242
pub fn push_all(&mut self, other: &[T]) {
249243
self.reserve_additional(other.len());
250244

251-
unsafe {
252-
unsafe_push_all_clone(self, other)
245+
for i in range(0, other.len()) {
246+
let len = self.len();
247+
248+
// Unsafe code so this can be optimised to a memcpy (or something similarly
249+
// fast) when T is Copy. LLVM is easily confused, so any extra operations
250+
// during the loop can prevent this optimisation.
251+
unsafe {
252+
ptr::write(
253+
self.as_mut_slice().unsafe_mut_ref(len),
254+
other.unsafe_ref(i).clone());
255+
self.set_len(len + 1);
256+
}
253257
}
254258
}
255259

@@ -1550,25 +1554,6 @@ pub mod raw {
15501554
}
15511555
}
15521556

1553-
// Unsafe code so this can be optimised to a memcpy (or something similarly
1554-
// fast) when T is Copy. LLVM is easily confused, so any extra operations
1555-
// during the loop can prevent this optimisation.
1556-
//
1557-
// WARNING: You must preallocate space on the vector before you call this
1558-
// method.
1559-
#[inline(always)]
1560-
unsafe fn unsafe_push_all_clone<T: Clone>(dst: &mut Vec<T>, src: &[T]) {
1561-
let mut dst_len = dst.len();
1562-
1563-
for i in range(0, src.len()) {
1564-
ptr::write(
1565-
dst.as_mut_slice().unsafe_mut_ref(dst_len),
1566-
src.unsafe_ref(i).clone());
1567-
dst_len += 1;
1568-
dst.set_len(dst_len);
1569-
}
1570-
}
1571-
15721557
#[cfg(test)]
15731558
mod tests {
15741559
extern crate test;

0 commit comments

Comments
 (0)