diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2017-08-21 22:20:00 -0700 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2017-08-21 22:20:00 -0700 |
| commit | c4cb2d1f2e74b4df4d9615b410ebc1c789c287dc (patch) | |
| tree | 0ca7bece42e436e7aab0f7d12ee7aedda67d2c83 /src/liballoc | |
| parent | 8df670b6a60f245d266dbd0b650a549b7b806bac (diff) | |
| download | rust-c4cb2d1f2e74b4df4d9615b410ebc1c789c287dc.tar.gz rust-c4cb2d1f2e74b4df4d9615b410ebc1c789c287dc.zip | |
Add [T]::swap_with_slice
The safe version of a method from ptr, like [T]::copy_from_slice
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/lib.rs | 2 | ||||
| -rw-r--r-- | src/liballoc/slice.rs | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 227fcfabcf1..4e91be365e2 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -121,7 +121,7 @@ #![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 diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs index 356ca7a5f5e..cbf242e884a 100644 --- a/src/liballoc/slice.rs +++ b/src/liballoc/slice.rs @@ -1461,6 +1461,31 @@ impl<T> [T] { 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 |
