about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-08-25 10:20:15 +0000
committerbors <bors@rust-lang.org>2017-08-25 10:20:15 +0000
commitba65645c78a87f1fa5c6d7132130bb3175fbe68b (patch)
treeabbeb24770cb1c25e1dbc1f603988c5bf84f020d /src/libcore
parent32b50e280faf56f21cbd82d1cf82cb4795535143 (diff)
parentc4cb2d1f2e74b4df4d9615b410ebc1c789c287dc (diff)
downloadrust-ba65645c78a87f1fa5c6d7132130bb3175fbe68b.tar.gz
rust-ba65645c78a87f1fa5c6d7132130bb3175fbe68b.zip
Auto merge of #44031 - scottmcm:swap_with_slice, r=alexcrichton
Add [T]::swap_with_slice

The safe version of a method from `ptr`, like `[T]::copy_from_slice` is.

Tracking issue: https://github.com/rust-lang/rust/issues/44030
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/slice/mod.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs
index 0509936153c..31d8266510a 100644
--- a/src/libcore/slice/mod.rs
+++ b/src/libcore/slice/mod.rs
@@ -212,6 +212,9 @@ pub trait SliceExt {
     #[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;
@@ -674,6 +677,16 @@ impl<T> SliceExt for [T] {
     }
 
     #[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,
               B: Borrow<Q>,