Skip to content

Commit

Permalink
Replace ad hoc implementations with slice::check_range
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Aug 17, 2020
1 parent ed02b90 commit d04e6b8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 90 deletions.
37 changes: 11 additions & 26 deletions library/alloc/src/collections/vec_deque.rs
Expand Up @@ -14,8 +14,7 @@ use core::fmt;
use core::hash::{Hash, Hasher};
use core::iter::{once, repeat_with, FromIterator, FusedIterator};
use core::mem::{self, replace, ManuallyDrop};
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{Index, IndexMut, RangeBounds, Try};
use core::ops::{Index, IndexMut, Range, RangeBounds, Try};
use core::ptr::{self, NonNull};
use core::slice;

Expand Down Expand Up @@ -1083,24 +1082,16 @@ impl<T> VecDeque<T> {
self.tail == self.head
}

fn range_start_end<R>(&self, range: R) -> (usize, usize)
fn range_tail_head<R>(&self, range: R) -> (usize, usize)
where
R: RangeBounds<usize>,
{
let len = self.len();
let start = match range.start_bound() {
Included(&n) => n,
Excluded(&n) => n + 1,
Unbounded => 0,
};
let end = match range.end_bound() {
Included(&n) => n + 1,
Excluded(&n) => n,
Unbounded => len,
};
assert!(start <= end, "lower bound was too large");
assert!(end <= len, "upper bound was too large");
(start, end)
// SAFETY: This buffer is only used to check the range.
let buffer = unsafe { slice::from_raw_parts(self.ptr(), self.len()) };
let Range { start, end } = buffer.check_range(range);
let tail = self.wrap_add(self.tail, start);
let head = self.wrap_add(self.tail, end);
(tail, head)
}

/// Creates an iterator that covers the specified range in the `VecDeque`.
Expand Down Expand Up @@ -1131,9 +1122,7 @@ impl<T> VecDeque<T> {
where
R: RangeBounds<usize>,
{
let (start, end) = self.range_start_end(range);
let tail = self.wrap_add(self.tail, start);
let head = self.wrap_add(self.tail, end);
let (tail, head) = self.range_tail_head(range);
Iter {
tail,
head,
Expand Down Expand Up @@ -1174,9 +1163,7 @@ impl<T> VecDeque<T> {
where
R: RangeBounds<usize>,
{
let (start, end) = self.range_start_end(range);
let tail = self.wrap_add(self.tail, start);
let head = self.wrap_add(self.tail, end);
let (tail, head) = self.range_tail_head(range);
IterMut {
tail,
head,
Expand Down Expand Up @@ -1230,7 +1217,7 @@ impl<T> VecDeque<T> {
// When finished, the remaining data will be copied back to cover the hole,
// and the head/tail values will be restored correctly.
//
let (start, end) = self.range_start_end(range);
let (drain_tail, drain_head) = self.range_tail_head(range);

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

// "forget" about the values after the start of the drain until after
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Expand Up @@ -114,6 +114,7 @@
#![feature(rustc_attrs)]
#![feature(receiver_trait)]
#![feature(min_specialization)]
#![feature(slice_check_range)]
#![feature(slice_ptr_get)]
#![feature(slice_ptr_len)]
#![feature(staged_api)]
Expand Down
20 changes: 6 additions & 14 deletions library/alloc/src/string.rs
Expand Up @@ -47,7 +47,7 @@ use core::fmt;
use core::hash;
use core::iter::{FromIterator, FusedIterator};
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeBounds};
use core::ops::{self, Add, AddAssign, Index, IndexMut, Range, RangeBounds};
use core::ptr;
use core::str::{lossy, pattern::Pattern};

Expand Down Expand Up @@ -1506,23 +1506,15 @@ impl String {
// of the vector version. The data is just plain bytes.
// Because the range removal happens in Drop, if the Drain iterator is leaked,
// the removal will not happen.
let len = self.len();
let start = match range.start_bound() {
Included(&n) => n,
Excluded(&n) => n + 1,
Unbounded => 0,
};
let end = match range.end_bound() {
Included(&n) => n + 1,
Excluded(&n) => n,
Unbounded => len,
};
let Range { start, end } = self.as_bytes().check_range(range);
assert!(self.is_char_boundary(start));
assert!(self.is_char_boundary(end));

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

Drain { start, end, iter: chars_iter, string: self_ptr }
}
Expand Down
33 changes: 2 additions & 31 deletions library/alloc/src/vec.rs
Expand Up @@ -66,8 +66,7 @@ use core::intrinsics::{arith_offset, assume};
use core::iter::{FromIterator, FusedIterator, TrustedLen};
use core::marker::PhantomData;
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{self, Index, IndexMut, RangeBounds};
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
use core::ptr::{self, NonNull};
use core::slice::{self, SliceIndex};

Expand Down Expand Up @@ -1311,35 +1310,7 @@ impl<T> Vec<T> {
// the hole, and the vector length is restored to the new length.
//
let len = self.len();
let start = match range.start_bound() {
Included(&n) => n,
Excluded(&n) => n + 1,
Unbounded => 0,
};
let end = match range.end_bound() {
Included(&n) => n + 1,
Excluded(&n) => n,
Unbounded => len,
};

#[cold]
#[inline(never)]
fn start_assert_failed(start: usize, end: usize) -> ! {
panic!("start drain index (is {}) should be <= end drain index (is {})", start, end);
}

#[cold]
#[inline(never)]
fn end_assert_failed(end: usize, len: usize) -> ! {
panic!("end drain index (is {}) should be <= len (is {})", end, len);
}

if start > end {
start_assert_failed(start, end);
}
if end > len {
end_assert_failed(end, len);
}
let Range { start, end } = self.check_range(range);

unsafe {
// set self.vec length's to start, to be safe in case Drain is leaked
Expand Down
19 changes: 2 additions & 17 deletions library/core/src/slice/mod.rs
Expand Up @@ -2511,26 +2511,11 @@ impl<T> [T] {
/// ```
#[stable(feature = "copy_within", since = "1.37.0")]
#[track_caller]
pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize)
pub fn copy_within<R: RangeBounds<usize>>(&mut self, src: R, dest: usize)
where
T: Copy,
{
let src_start = match src.start_bound() {
ops::Bound::Included(&n) => n,
ops::Bound::Excluded(&n) => {
n.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
}
ops::Bound::Unbounded => 0,
};
let src_end = match src.end_bound() {
ops::Bound::Included(&n) => {
n.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
}
ops::Bound::Excluded(&n) => n,
ops::Bound::Unbounded => self.len(),
};
assert!(src_start <= src_end, "src end is before src start");
assert!(src_end <= self.len(), "src is out of bounds");
let Range { start: src_start, end: src_end } = self.check_range(src);
let count = src_end - src_start;
assert!(dest <= self.len() - count, "dest is out of bounds");
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions library/core/tests/slice.rs
Expand Up @@ -1797,7 +1797,7 @@ fn test_copy_within() {
}

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

0 comments on commit d04e6b8

Please sign in to comment.