From c4cb2d1f2e74b4df4d9615b410ebc1c789c287dc Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Mon, 21 Aug 2017 22:20:00 -0700 Subject: Add [T]::swap_with_slice The safe version of a method from ptr, like [T]::copy_from_slice --- src/libcore/slice/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/libcore') 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; @@ -673,6 +676,16 @@ impl 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 where F: FnMut(&'a Self::Item) -> B, -- cgit 1.4.1-3-g733a5