Skip to content

Commit

Permalink
Rollup merge of rust-lang#61398 - kennytm:stabilize-copy-within, r=Si…
Browse files Browse the repository at this point in the history
…monSapin

Stabilize copy_within

Closes rust-lang#54236.
  • Loading branch information
Centril committed Jun 12, 2019
2 parents 20858c4 + 427f1a4 commit 648a529
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/libcore/slice/mod.rs
Expand Up @@ -2146,14 +2146,13 @@ impl<T> [T] {
/// Copying four bytes within a slice: /// Copying four bytes within a slice:
/// ///
/// ``` /// ```
/// # #![feature(copy_within)]
/// let mut bytes = *b"Hello, World!"; /// let mut bytes = *b"Hello, World!";
/// ///
/// bytes.copy_within(1..5, 8); /// bytes.copy_within(1..5, 8);
/// ///
/// assert_eq!(&bytes, b"Hello, Wello!"); /// assert_eq!(&bytes, b"Hello, Wello!");
/// ``` /// ```
#[unstable(feature = "copy_within", issue = "54236")] #[stable(feature = "copy_within", since = "1.37.0")]
pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize) pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize)
where where
T: Copy, T: Copy,
Expand All @@ -2178,8 +2177,8 @@ impl<T> [T] {
assert!(dest <= self.len() - count, "dest is out of bounds"); assert!(dest <= self.len() - count, "dest is out of bounds");
unsafe { unsafe {
ptr::copy( ptr::copy(
self.get_unchecked(src_start), self.as_ptr().add(src_start),
self.get_unchecked_mut(dest), self.as_mut_ptr().add(dest),
count, count,
); );
} }
Expand Down
1 change: 0 additions & 1 deletion src/libcore/tests/lib.rs
Expand Up @@ -28,7 +28,6 @@
#![feature(inner_deref)] #![feature(inner_deref)]
#![feature(slice_internals)] #![feature(slice_internals)]
#![feature(slice_partition_dedup)] #![feature(slice_partition_dedup)]
#![feature(copy_within)]
#![feature(int_error_matching)] #![feature(int_error_matching)]
#![feature(const_fn)] #![feature(const_fn)]
#![warn(rust_2018_idioms)] #![warn(rust_2018_idioms)]
Expand Down
14 changes: 14 additions & 0 deletions src/libcore/tests/slice.rs
Expand Up @@ -1512,6 +1512,13 @@ fn test_copy_within() {
let mut bytes = *b"Hello, World!"; let mut bytes = *b"Hello, World!";
bytes.copy_within(.., 0); bytes.copy_within(.., 0);
assert_eq!(&bytes, b"Hello, World!"); assert_eq!(&bytes, b"Hello, World!");

// Ensure that copying at the end of slice won't cause UB.
let mut bytes = *b"Hello, World!";
bytes.copy_within(13..13, 5);
assert_eq!(&bytes, b"Hello, World!");
bytes.copy_within(5..5, 13);
assert_eq!(&bytes, b"Hello, World!");
} }


#[test] #[test]
Expand All @@ -1536,6 +1543,13 @@ fn test_copy_within_panics_src_inverted() {
// 2 is greater than 1, so this range is invalid. // 2 is greater than 1, so this range is invalid.
bytes.copy_within(2..1, 0); bytes.copy_within(2..1, 0);
} }
#[test]
#[should_panic(expected = "attempted to index slice up to maximum usize")]
fn test_copy_within_panics_src_out_of_bounds() {
let mut bytes = *b"Hello, World!";
// an inclusive range ending at usize::max_value() would make src_end overflow
bytes.copy_within(usize::max_value()..=usize::max_value(), 0);
}


#[test] #[test]
fn test_is_sorted() { fn test_is_sorted() {
Expand Down

0 comments on commit 648a529

Please sign in to comment.