Skip to content

Commit a1ff450

Browse files
authored
Rollup merge of rust-lang#61398 - kennytm:stabilize-copy-within, r=SimonSapin
Stabilize copy_within Closes rust-lang#54236.
2 parents b35aeae + 427f1a4 commit a1ff450

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/libcore/slice/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2146,14 +2146,13 @@ impl<T> [T] {
21462146
/// Copying four bytes within a slice:
21472147
///
21482148
/// ```
2149-
/// # #![feature(copy_within)]
21502149
/// let mut bytes = *b"Hello, World!";
21512150
///
21522151
/// bytes.copy_within(1..5, 8);
21532152
///
21542153
/// assert_eq!(&bytes, b"Hello, Wello!");
21552154
/// ```
2156-
#[unstable(feature = "copy_within", issue = "54236")]
2155+
#[stable(feature = "copy_within", since = "1.37.0")]
21572156
pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize)
21582157
where
21592158
T: Copy,
@@ -2178,8 +2177,8 @@ impl<T> [T] {
21782177
assert!(dest <= self.len() - count, "dest is out of bounds");
21792178
unsafe {
21802179
ptr::copy(
2181-
self.get_unchecked(src_start),
2182-
self.get_unchecked_mut(dest),
2180+
self.as_ptr().add(src_start),
2181+
self.as_mut_ptr().add(dest),
21832182
count,
21842183
);
21852184
}

src/libcore/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#![feature(inner_deref)]
2929
#![feature(slice_internals)]
3030
#![feature(slice_partition_dedup)]
31-
#![feature(copy_within)]
3231
#![feature(int_error_matching)]
3332
#![feature(const_fn)]
3433
#![warn(rust_2018_idioms)]

src/libcore/tests/slice.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,13 @@ fn test_copy_within() {
15121512
let mut bytes = *b"Hello, World!";
15131513
bytes.copy_within(.., 0);
15141514
assert_eq!(&bytes, b"Hello, World!");
1515+
1516+
// Ensure that copying at the end of slice won't cause UB.
1517+
let mut bytes = *b"Hello, World!";
1518+
bytes.copy_within(13..13, 5);
1519+
assert_eq!(&bytes, b"Hello, World!");
1520+
bytes.copy_within(5..5, 13);
1521+
assert_eq!(&bytes, b"Hello, World!");
15151522
}
15161523

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

15401554
#[test]
15411555
fn test_is_sorted() {

0 commit comments

Comments
 (0)