]> git.lizzy.rs Git - rust.git/commitdiff
Add [T]::swap_with_slice
authorScott McMurray <scottmcm@users.noreply.github.com>
Tue, 22 Aug 2017 05:20:00 +0000 (22:20 -0700)
committerScott McMurray <scottmcm@users.noreply.github.com>
Tue, 22 Aug 2017 05:20:00 +0000 (22:20 -0700)
The safe version of a method from ptr, like [T]::copy_from_slice

src/liballoc/lib.rs
src/liballoc/slice.rs
src/libcore/slice/mod.rs

index 227fcfabcf11de25056fbdf7918a7ae004853dcb..4e91be365e2b91ec4afa9be0e7671d6bcb642a77 100644 (file)
 #![feature(unsize)]
 #![feature(allocator_internals)]
 
-#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol))]
+#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, swap_with_slice))]
 #![cfg_attr(test, feature(test, box_heap))]
 
 // Allow testing this library
index 356ca7a5f5e0cee6adf9372629a330b886cc3774..cbf242e884a70cda7258c98aa969b1f783b028ba 100644 (file)
@@ -1461,6 +1461,31 @@ pub fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
         core_slice::SliceExt::copy_from_slice(self, src)
     }
 
+    /// Swaps all elements in `self` with those in `src`.
+    ///
+    /// The length of `src` must be the same as `self`.
+    ///
+    /// # Panics
+    ///
+    /// This function will panic if the two slices have different lengths.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// #![feature(swap_with_slice)]
+    ///
+    /// let mut src = [1, 2, 3];
+    /// let mut dst = [7, 8, 9];
+    ///
+    /// src.swap_with_slice(&mut dst);
+    /// assert_eq!(src, [7, 8, 9]);
+    /// assert_eq!(dst, [1, 2, 3]);
+    /// ```
+    #[unstable(feature = "swap_with_slice", issue = "44030")]
+    pub fn swap_with_slice(&mut self, src: &mut [T]) {
+        core_slice::SliceExt::swap_with_slice(self, src)
+    }
+
     /// Copies `self` into a new `Vec`.
     ///
     /// # Examples
index 0509936153c39c5d244ca0be69c8991eb1d2ceb9..31d8266510a1ea652cb400e407829307f90938b5 100644 (file)
@@ -212,6 +212,9 @@ unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
     #[stable(feature = "copy_from_slice", since = "1.9.0")]
     fn copy_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Copy;
 
+    #[unstable(feature = "swap_with_slice", issue = "44030")]
+    fn swap_with_slice(&mut self, src: &mut [Self::Item]);
+
     #[stable(feature = "sort_unstable", since = "1.20.0")]
     fn sort_unstable(&mut self)
         where Self::Item: Ord;
@@ -673,6 +676,16 @@ fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
         }
     }
 
+    #[inline]
+    fn swap_with_slice(&mut self, src: &mut [T]) {
+        assert!(self.len() == src.len(),
+                "destination and source slices have different lengths");
+        unsafe {
+            ptr::swap_nonoverlapping(
+                self.as_mut_ptr(), src.as_mut_ptr(), self.len());
+        }
+    }
+
     #[inline]
     fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, mut f: F) -> Result<usize, usize>
         where F: FnMut(&'a Self::Item) -> B,