Skip to content

Commit d04e6b8

Browse files
committed
Replace ad hoc implementations with slice::check_range
1 parent ed02b90 commit d04e6b8

File tree

6 files changed

+24
-90
lines changed

6 files changed

+24
-90
lines changed

library/alloc/src/collections/vec_deque.rs

+11-26
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use core::fmt;
1414
use core::hash::{Hash, Hasher};
1515
use core::iter::{once, repeat_with, FromIterator, FusedIterator};
1616
use core::mem::{self, replace, ManuallyDrop};
17-
use core::ops::Bound::{Excluded, Included, Unbounded};
18-
use core::ops::{Index, IndexMut, RangeBounds, Try};
17+
use core::ops::{Index, IndexMut, Range, RangeBounds, Try};
1918
use core::ptr::{self, NonNull};
2019
use core::slice;
2120

@@ -1083,24 +1082,16 @@ impl<T> VecDeque<T> {
10831082
self.tail == self.head
10841083
}
10851084

1086-
fn range_start_end<R>(&self, range: R) -> (usize, usize)
1085+
fn range_tail_head<R>(&self, range: R) -> (usize, usize)
10871086
where
10881087
R: RangeBounds<usize>,
10891088
{
1090-
let len = self.len();
1091-
let start = match range.start_bound() {
1092-
Included(&n) => n,
1093-
Excluded(&n) => n + 1,
1094-
Unbounded => 0,
1095-
};
1096-
let end = match range.end_bound() {
1097-
Included(&n) => n + 1,
1098-
Excluded(&n) => n,
1099-
Unbounded => len,
1100-
};
1101-
assert!(start <= end, "lower bound was too large");
1102-
assert!(end <= len, "upper bound was too large");
1103-
(start, end)
1089+
// SAFETY: This buffer is only used to check the range.
1090+
let buffer = unsafe { slice::from_raw_parts(self.ptr(), self.len()) };
1091+
let Range { start, end } = buffer.check_range(range);
1092+
let tail = self.wrap_add(self.tail, start);
1093+
let head = self.wrap_add(self.tail, end);
1094+
(tail, head)
11041095
}
11051096

11061097
/// Creates an iterator that covers the specified range in the `VecDeque`.
@@ -1131,9 +1122,7 @@ impl<T> VecDeque<T> {
11311122
where
11321123
R: RangeBounds<usize>,
11331124
{
1134-
let (start, end) = self.range_start_end(range);
1135-
let tail = self.wrap_add(self.tail, start);
1136-
let head = self.wrap_add(self.tail, end);
1125+
let (tail, head) = self.range_tail_head(range);
11371126
Iter {
11381127
tail,
11391128
head,
@@ -1174,9 +1163,7 @@ impl<T> VecDeque<T> {
11741163
where
11751164
R: RangeBounds<usize>,
11761165
{
1177-
let (start, end) = self.range_start_end(range);
1178-
let tail = self.wrap_add(self.tail, start);
1179-
let head = self.wrap_add(self.tail, end);
1166+
let (tail, head) = self.range_tail_head(range);
11801167
IterMut {
11811168
tail,
11821169
head,
@@ -1230,7 +1217,7 @@ impl<T> VecDeque<T> {
12301217
// When finished, the remaining data will be copied back to cover the hole,
12311218
// and the head/tail values will be restored correctly.
12321219
//
1233-
let (start, end) = self.range_start_end(range);
1220+
let (drain_tail, drain_head) = self.range_tail_head(range);
12341221

12351222
// The deque's elements are parted into three segments:
12361223
// * self.tail -> drain_tail
@@ -1248,8 +1235,6 @@ impl<T> VecDeque<T> {
12481235
// T t h H
12491236
// [. . . o o x x o o . . .]
12501237
//
1251-
let drain_tail = self.wrap_add(self.tail, start);
1252-
let drain_head = self.wrap_add(self.tail, end);
12531238
let head = self.head;
12541239

12551240
// "forget" about the values after the start of the drain until after

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
#![feature(rustc_attrs)]
115115
#![feature(receiver_trait)]
116116
#![feature(min_specialization)]
117+
#![feature(slice_check_range)]
117118
#![feature(slice_ptr_get)]
118119
#![feature(slice_ptr_len)]
119120
#![feature(staged_api)]

library/alloc/src/string.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use core::fmt;
4747
use core::hash;
4848
use core::iter::{FromIterator, FusedIterator};
4949
use core::ops::Bound::{Excluded, Included, Unbounded};
50-
use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeBounds};
50+
use core::ops::{self, Add, AddAssign, Index, IndexMut, Range, RangeBounds};
5151
use core::ptr;
5252
use core::str::{lossy, pattern::Pattern};
5353

@@ -1506,23 +1506,15 @@ impl String {
15061506
// of the vector version. The data is just plain bytes.
15071507
// Because the range removal happens in Drop, if the Drain iterator is leaked,
15081508
// the removal will not happen.
1509-
let len = self.len();
1510-
let start = match range.start_bound() {
1511-
Included(&n) => n,
1512-
Excluded(&n) => n + 1,
1513-
Unbounded => 0,
1514-
};
1515-
let end = match range.end_bound() {
1516-
Included(&n) => n + 1,
1517-
Excluded(&n) => n,
1518-
Unbounded => len,
1519-
};
1509+
let Range { start, end } = self.as_bytes().check_range(range);
1510+
assert!(self.is_char_boundary(start));
1511+
assert!(self.is_char_boundary(end));
15201512

15211513
// Take out two simultaneous borrows. The &mut String won't be accessed
15221514
// until iteration is over, in Drop.
15231515
let self_ptr = self as *mut _;
1524-
// slicing does the appropriate bounds checks
1525-
let chars_iter = self[start..end].chars();
1516+
// SAFETY: `check_range` and `is_char_boundary` do the appropriate bounds checks.
1517+
let chars_iter = unsafe { self.get_unchecked(start..end) }.chars();
15261518

15271519
Drain { start, end, iter: chars_iter, string: self_ptr }
15281520
}

library/alloc/src/vec.rs

+2-31
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ use core::intrinsics::{arith_offset, assume};
6666
use core::iter::{FromIterator, FusedIterator, TrustedLen};
6767
use core::marker::PhantomData;
6868
use core::mem::{self, ManuallyDrop, MaybeUninit};
69-
use core::ops::Bound::{Excluded, Included, Unbounded};
70-
use core::ops::{self, Index, IndexMut, RangeBounds};
69+
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
7170
use core::ptr::{self, NonNull};
7271
use core::slice::{self, SliceIndex};
7372

@@ -1311,35 +1310,7 @@ impl<T> Vec<T> {
13111310
// the hole, and the vector length is restored to the new length.
13121311
//
13131312
let len = self.len();
1314-
let start = match range.start_bound() {
1315-
Included(&n) => n,
1316-
Excluded(&n) => n + 1,
1317-
Unbounded => 0,
1318-
};
1319-
let end = match range.end_bound() {
1320-
Included(&n) => n + 1,
1321-
Excluded(&n) => n,
1322-
Unbounded => len,
1323-
};
1324-
1325-
#[cold]
1326-
#[inline(never)]
1327-
fn start_assert_failed(start: usize, end: usize) -> ! {
1328-
panic!("start drain index (is {}) should be <= end drain index (is {})", start, end);
1329-
}
1330-
1331-
#[cold]
1332-
#[inline(never)]
1333-
fn end_assert_failed(end: usize, len: usize) -> ! {
1334-
panic!("end drain index (is {}) should be <= len (is {})", end, len);
1335-
}
1336-
1337-
if start > end {
1338-
start_assert_failed(start, end);
1339-
}
1340-
if end > len {
1341-
end_assert_failed(end, len);
1342-
}
1313+
let Range { start, end } = self.check_range(range);
13431314

13441315
unsafe {
13451316
// set self.vec length's to start, to be safe in case Drain is leaked

library/core/src/slice/mod.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -2511,26 +2511,11 @@ impl<T> [T] {
25112511
/// ```
25122512
#[stable(feature = "copy_within", since = "1.37.0")]
25132513
#[track_caller]
2514-
pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize)
2514+
pub fn copy_within<R: RangeBounds<usize>>(&mut self, src: R, dest: usize)
25152515
where
25162516
T: Copy,
25172517
{
2518-
let src_start = match src.start_bound() {
2519-
ops::Bound::Included(&n) => n,
2520-
ops::Bound::Excluded(&n) => {
2521-
n.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
2522-
}
2523-
ops::Bound::Unbounded => 0,
2524-
};
2525-
let src_end = match src.end_bound() {
2526-
ops::Bound::Included(&n) => {
2527-
n.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
2528-
}
2529-
ops::Bound::Excluded(&n) => n,
2530-
ops::Bound::Unbounded => self.len(),
2531-
};
2532-
assert!(src_start <= src_end, "src end is before src start");
2533-
assert!(src_end <= self.len(), "src is out of bounds");
2518+
let Range { start: src_start, end: src_end } = self.check_range(src);
25342519
let count = src_end - src_start;
25352520
assert!(dest <= self.len() - count, "dest is out of bounds");
25362521
unsafe {

library/core/tests/slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ fn test_copy_within() {
17971797
}
17981798

17991799
#[test]
1800-
#[should_panic(expected = "src is out of bounds")]
1800+
#[should_panic(expected = "range end index 14 out of range for slice of length 13")]
18011801
fn test_copy_within_panics_src_too_long() {
18021802
let mut bytes = *b"Hello, World!";
18031803
// The length is only 13, so 14 is out of bounds.
@@ -1812,7 +1812,7 @@ fn test_copy_within_panics_dest_too_long() {
18121812
bytes.copy_within(0..4, 10);
18131813
}
18141814
#[test]
1815-
#[should_panic(expected = "src end is before src start")]
1815+
#[should_panic(expected = "slice index starts at 2 but ends at 1")]
18161816
fn test_copy_within_panics_src_inverted() {
18171817
let mut bytes = *b"Hello, World!";
18181818
// 2 is greater than 1, so this range is invalid.

0 commit comments

Comments
 (0)