// option. This file may not be copied, modified, or distributed
// except according to those terms.
-//! A growable list type, written `Vec<T>` but pronounced 'vector.'
+//! A growable list type with heap-allocated contents, written `Vec<T>` but pronounced 'vector.'
//!
//! Vectors have `O(1)` indexing, push (to the end) and pop (from the end).
//!
}
}
+ /// 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.
///
}
}
-#[cfg(stage0)]
-impl<S: hash::Writer, T: Hash<S>> Hash<S> for Vec<T> {
- #[inline]
- fn hash(&self, state: &mut S) {
- self.as_slice().hash(state);
- }
-}
-#[cfg(not(stage0))]
impl<S: hash::Writer + hash::Hasher, T: Hash<S>> Hash<S> for Vec<T> {
#[inline]
fn hash(&self, state: &mut S) {
#[should_fail]
fn test_slice_out_of_bounds_1() {
let x: Vec<int> = vec![1, 2, 3, 4, 5];
- &x[(-1)..];
+ &x[-1..];
}
#[test]
#[should_fail]
fn test_slice_out_of_bounds_3() {
let x: Vec<int> = vec![1, 2, 3, 4, 5];
- &x[(-1)..4];
+ &x[-1..4];
}
#[test]
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(|| {