]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/vec.rs
remove unnecessary parentheses from range notation
[rust.git] / src / libcollections / vec.rs
index 8f6c343205d6c7086a4aed00244d0b8d23bc94f8..27232007d923b03c07b5ba974d64b45f57cb780f 100644 (file)
@@ -8,7 +8,7 @@
 // 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).
 //!
@@ -681,6 +681,43 @@ pub fn pop(&mut self) -> Option<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.
     ///
@@ -1185,14 +1222,6 @@ fn clone_from(&mut self, other: &Vec<T>) {
     }
 }
 
-#[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) {
@@ -2129,7 +2158,7 @@ fn test_index_out_of_bounds() {
     #[should_fail]
     fn test_slice_out_of_bounds_1() {
         let x: Vec<int> = vec![1, 2, 3, 4, 5];
-        &x[(-1)..];
+        &x[-1..];
     }
 
     #[test]
@@ -2143,7 +2172,7 @@ fn test_slice_out_of_bounds_2() {
     #[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]
@@ -2298,6 +2327,15 @@ fn test_into_boxed_slice() {
         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(|| {