Skip to content

Commit

Permalink
add append to vec with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBelgum committed Jan 18, 2015
1 parent f4f10db commit 08e712e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/libcollections/vec.rs
Expand Up @@ -681,6 +681,43 @@ impl<T> Vec<T> {
}
}

/// Moves all the elements of `other` into `Self`, leaving `other` empty.
///
/// # Panics
///
/// Panics if the number of elements in the vector overflows a `uint`.
///
/// # Examples
/// ```rust
/// let mut vec = vec![1, 2, 3];
/// let mut vec2 = vec![4, 5, 6];
/// vec.append(&mut vec2);
/// assert_eq!(vec, vec![1, 2, 3, 4, 5, 6]);
/// assert_eq!(vec2, vec![]);
/// ```
#[inline]
#[unstable = "new API, waiting for dust to settle"]
pub fn append(&mut self, other: &mut Self) {
if mem::size_of::<T>() == 0 {
// zero-size types consume no memory, so we can't rely on the
// address space running out
self.len = self.len.checked_add(other.len()).expect("length overflow");
unsafe { other.set_len(0) }
return;
}
self.reserve(other.len());
let len = self.len();
unsafe {
ptr::copy_nonoverlapping_memory(
self.get_unchecked_mut(len),
other.as_ptr(),
other.len());
}

self.len += other.len();
unsafe { other.set_len(0); }
}

/// Creates a draining iterator that clears the `Vec` and iterates over
/// the removed items from start to end.
///
Expand Down Expand Up @@ -2298,6 +2335,15 @@ mod tests {
assert_eq!(ys.as_slice(), [1u, 2, 3]);
}

#[test]
fn test_append() {
let mut vec = vec![1, 2, 3];
let mut vec2 = vec![4, 5, 6];
vec.append(&mut vec2);
assert_eq!(vec, vec![1, 2, 3, 4, 5, 6]);
assert_eq!(vec2, vec![]);
}

#[bench]
fn bench_new(b: &mut Bencher) {
b.iter(|| {
Expand Down

8 comments on commit 08e712e

@Gankra
Copy link
Contributor

@Gankra Gankra commented on 08e712e Jan 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+ rollup

@bors
Copy link
Contributor

@bors bors commented on 08e712e Jan 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Gankro
at JeffBelgum@08e712e

@bors
Copy link
Contributor

@bors bors commented on 08e712e Jan 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging JeffBelgum/rust/collections-reform-issue-19986-add-append-and-split-off = 08e712e into auto

@bors
Copy link
Contributor

@bors bors commented on 08e712e Jan 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status: {"merge_sha": "6da8827482418b6ee89eaf3c66b8693c4cc649e8"}

@bors
Copy link
Contributor

@bors bors commented on 08e712e Jan 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JeffBelgum/rust/collections-reform-issue-19986-add-append-and-split-off = 08e712e merged ok, testing candidate = 6da8827

@bors
Copy link
Contributor

@bors bors commented on 08e712e Jan 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 6da8827

@bors
Copy link
Contributor

@bors bors commented on 08e712e Jan 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 6da8827

Please sign in to comment.