Skip to content

Commit

Permalink
collections: minimize code that's in unsafe blocks
Browse files Browse the repository at this point in the history
This changes Vec::from_slice to call unsafe_push_all_clone
directly to avoid doing an unnecessary reserve_additional call
  • Loading branch information
erickt committed Jul 7, 2014
1 parent 7d38994 commit e2d107c
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/libcollections/vec.rs
Expand Up @@ -198,7 +198,13 @@ impl<T: Clone> Vec<T> {
#[inline]
pub fn from_slice(values: &[T]) -> Vec<T> {
let mut vector = Vec::with_capacity(values.len());
vector.push_all(values);

// Directly call `unsafe_push_all_clone` so we can skip a call to
// `reserve_addtional`.
unsafe {
unsafe_push_all_clone(&mut vector, values);
}

vector
}

Expand Down Expand Up @@ -240,8 +246,9 @@ impl<T: Clone> Vec<T> {
/// ```
#[inline]
pub fn push_all(&mut self, other: &[T]) {
self.reserve_additional(other.len());

unsafe {
self.reserve_additional(other.len());
unsafe_push_all_clone(self, other)
}
}
Expand Down Expand Up @@ -323,31 +330,24 @@ impl<T: Clone> Vec<T> {
#[unstable]
impl<T:Clone> Clone for Vec<T> {
fn clone(&self) -> Vec<T> {
unsafe {
let mut vector = Vec::with_capacity(self.len);
unsafe_push_all_clone(&mut vector, self.as_slice());
vector
}
Vec::from_slice(self.as_slice())
}

fn clone_from(&mut self, other: &Vec<T>) {
unsafe {
// drop anything in self that will not be overwritten
if self.len() > other.len() {
self.truncate(other.len())
}

// reuse the contained values' allocations/resources.
for (place, thing) in self.mut_iter().zip(other.iter()) {
place.clone_from(thing)
}
// drop anything in self that will not be overwritten
if self.len() > other.len() {
self.truncate(other.len())
}

// self.len <= other.len due to the truncate above, so the
// slice here is always in-bounds.
let slice = other.slice_from(self.len());
self.reserve_additional(slice.len());
unsafe_push_all_clone(self, slice)
// reuse the contained values' allocations/resources.
for (place, thing) in self.mut_iter().zip(other.iter()) {
place.clone_from(thing)
}

// self.len <= other.len due to the truncate above, so the
// slice here is always in-bounds.
let slice = other.slice_from(self.len());
self.push_all(slice);
}
}

Expand Down

0 comments on commit e2d107c

Please sign in to comment.