Skip to content

Add [T]::swap_with_slice #44031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -673,6 +676,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,
Expand Down