Skip to content

add append to vec with tests and benchmarks #21330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
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