]> git.lizzy.rs Git - rust.git/commitdiff
Add unstable copy_from_slice
authorNicholas Mazzuca <npmazzuca@gmail.com>
Tue, 23 Feb 2016 07:06:53 +0000 (23:06 -0800)
committerNicholas Mazzuca <npmazzuca@gmail.com>
Fri, 26 Feb 2016 05:20:41 +0000 (21:20 -0800)
src/libcollections/lib.rs
src/libcollections/slice.rs
src/libcollectionstest/lib.rs
src/libcollectionstest/slice.rs
src/libcore/slice.rs

index abf50a5fe3ec10604d431bdbc7dfebb0e17b53c8..9c6fdc217dc42ba8d0b7bdb9b9e86e1056dfc7ef 100644 (file)
@@ -27,6 +27,7 @@
        test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
 
 #![cfg_attr(test, allow(deprecated))] // rand
+#![cfg_attr(not(test), feature(copy_from_slice))] // impl [T]
 #![cfg_attr(not(stage0), deny(warnings))]
 
 #![feature(alloc)]
index 6252e4888eb3ca71f6d37e9e2a9df764d443607b..21c5e71286192fda7ae93450ec811e22d9fb562c 100644 (file)
@@ -837,6 +837,30 @@ pub fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
         core_slice::SliceExt::clone_from_slice(self, src)
     }
 
+    /// Copies all elements from `src` into `self`, using a memcpy.
+    ///
+    /// The length of `src` must be the same as `self`.
+    ///
+    /// # Panics
+    ///
+    /// This function will panic if the two slices have different lengths.
+    ///
+    /// # Example
+    ///
+    /// ```rust
+    /// #![feature(copy_from_slice)]
+    /// let mut dst = [0, 0, 0];
+    /// let src = [1, 2, 3];
+    ///
+    /// dst.copy_from_slice(&src);
+    /// assert_eq!(src, dst);
+    /// ```
+    #[unstable(feature = "copy_from_slice", issue = "31755")]
+    pub fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
+        core_slice::SliceExt::copy_from_slice(self, src)
+    }
+
+
     /// Copies `self` into a new `Vec`.
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
index 891ca22265e990b4aadd1d016588d9053c446831..bf76d0b847caa08b54b629492da9d9bf888ee64e 100644 (file)
@@ -16,6 +16,7 @@
 #![feature(btree_range)]
 #![feature(collections)]
 #![feature(collections_bound)]
+#![feature(copy_from_slice)]
 #![feature(const_fn)]
 #![feature(fn_traits)]
 #![feature(enumset)]
index cde7fcaaf51a29d5a4e113908ebb25fca527c024..f3bb58ba45b7effd4d3331634a180702bd0ff3fb 100644 (file)
@@ -1138,6 +1138,30 @@ fn clone(&self) -> Self {
     assert_eq!(drop_count.load(Ordering::SeqCst), 8);
 }
 
+#[test]
+fn test_copy_from_slice() {
+    let src = [0, 1, 2, 3, 4, 5];
+    let mut dst = [0; 6];
+    dst.copy_from_slice(&src);
+    assert_eq!(src, dst)
+}
+
+#[test]
+#[should_panic(expected = "destination and source slices have different lengths")]
+fn test_copy_from_slice_dst_longer() {
+    let src = [0, 1, 2, 3];
+    let mut dst = [0; 5];
+    dst.copy_from_slice(&src);
+}
+
+#[test]
+#[should_panic(expected = "destination and source slices have different lengths")]
+fn test_copy_from_slice_dst_shorter() {
+    let src = [0, 1, 2, 3];
+    let mut dst = [0; 3];
+    dst.copy_from_slice(&src);
+}
+
 mod bench {
     use std::{mem, ptr};
     use std::__rand::{Rng, thread_rng};
index 40041c748e7ffcd41dbb5ca4078995702ac2c3ff..afda70f4fcc0af67b8d44853f7002ce071fe1339 100644 (file)
@@ -48,7 +48,7 @@
 use result::Result::{Ok, Err};
 use ptr;
 use mem;
-use marker::{Send, Sync, self};
+use marker::{Copy, Send, Sync, self};
 use raw::Repr;
 // Avoid conflicts with *both* the Slice trait (buggy) and the `slice::raw` module.
 use raw::Slice as RawSlice;
@@ -152,6 +152,8 @@ fn rsplitn_mut<P>(&mut self,  n: usize, pred: P) -> RSplitNMut<Self::Item, P>
 
     #[stable(feature = "clone_from_slice", since = "1.7.0")]
     fn clone_from_slice(&mut self, &[Self::Item]) where Self::Item: Clone;
+    #[unstable(feature = "copy_from_slice", issue = "31755")]
+    fn copy_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Copy;
 }
 
 // Use macros to be generic over const/mut
@@ -488,6 +490,16 @@ fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
             self[i].clone_from(&src[i]);
         }
     }
+
+    #[inline]
+    fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
+        assert!(self.len() == src.len(),
+                "destination and source slices have different lengths");
+        unsafe {
+            ptr::copy_nonoverlapping(
+                src.as_ptr(), self.as_mut_ptr(), self.len());
+        }
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]