Skip to content

Commit 1ff7da6

Browse files
committed
Move slice::check_range to RangeBounds
1 parent 2c69266 commit 1ff7da6

File tree

10 files changed

+104
-101
lines changed

10 files changed

+104
-101
lines changed

library/alloc/src/collections/vec_deque.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ impl<T> VecDeque<T> {
10891089
where
10901090
R: RangeBounds<usize>,
10911091
{
1092-
let Range { start, end } = slice::check_range(self.len(), range);
1092+
let Range { start, end } = range.for_length(self.len());
10931093
let tail = self.wrap_add(self.tail, start);
10941094
let head = self.wrap_add(self.tail, end);
10951095
(tail, head)

library/alloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@
116116
#![feature(or_patterns)]
117117
#![feature(pattern)]
118118
#![feature(ptr_internals)]
119+
#![feature(range_bounds_for_length)]
119120
#![feature(raw_ref_op)]
120121
#![feature(rustc_attrs)]
121122
#![feature(receiver_trait)]
122123
#![feature(min_specialization)]
123-
#![feature(slice_check_range)]
124124
#![feature(slice_ptr_get)]
125125
#![feature(slice_ptr_len)]
126126
#![feature(staged_api)]

library/alloc/src/slice.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ use crate::borrow::ToOwned;
9191
use crate::boxed::Box;
9292
use crate::vec::Vec;
9393

94-
#[unstable(feature = "slice_check_range", issue = "76393")]
95-
pub use core::slice::check_range;
9694
#[unstable(feature = "array_chunks", issue = "74985")]
9795
pub use core::slice::ArrayChunks;
9896
#[unstable(feature = "array_chunks", issue = "74985")]

library/alloc/src/string.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use core::iter::{FromIterator, FusedIterator};
4949
use core::ops::Bound::{Excluded, Included, Unbounded};
5050
use core::ops::{self, Add, AddAssign, Index, IndexMut, Range, RangeBounds};
5151
use core::ptr;
52-
use core::slice;
5352
use core::str::{lossy, pattern::Pattern};
5453

5554
use crate::borrow::{Cow, ToOwned};
@@ -1507,14 +1506,14 @@ impl String {
15071506
// of the vector version. The data is just plain bytes.
15081507
// Because the range removal happens in Drop, if the Drain iterator is leaked,
15091508
// the removal will not happen.
1510-
let Range { start, end } = slice::check_range(self.len(), range);
1509+
let Range { start, end } = range.for_length(self.len());
15111510
assert!(self.is_char_boundary(start));
15121511
assert!(self.is_char_boundary(end));
15131512

15141513
// Take out two simultaneous borrows. The &mut String won't be accessed
15151514
// until iteration is over, in Drop.
15161515
let self_ptr = self as *mut _;
1517-
// SAFETY: `check_range` and `is_char_boundary` do the appropriate bounds checks.
1516+
// SAFETY: `for_length` and `is_char_boundary` do the appropriate bounds checks.
15181517
let chars_iter = unsafe { self.get_unchecked(start..end) }.chars();
15191518

15201519
Drain { start, end, iter: chars_iter, string: self_ptr }

library/alloc/src/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ impl<T> Vec<T> {
13121312
// the hole, and the vector length is restored to the new length.
13131313
//
13141314
let len = self.len();
1315-
let Range { start, end } = slice::check_range(len, range);
1315+
let Range { start, end } = range.for_length(len);
13161316

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

library/core/src/ops/range.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use crate::fmt;
22
use crate::hash::Hash;
3+
use crate::slice::index::{
4+
slice_end_index_len_fail, slice_end_index_overflow_fail, slice_index_order_fail,
5+
slice_start_index_overflow_fail,
6+
};
37

48
/// An unbounded range (`..`).
59
///
@@ -729,6 +733,84 @@ pub trait RangeBounds<T: ?Sized> {
729733
Unbounded => true,
730734
})
731735
}
736+
737+
/// Performs bounds-checking of this range.
738+
///
739+
/// The returned [`Range`] is safe to pass to [`slice::get_unchecked`] and
740+
/// [`slice::get_unchecked_mut`] for slices of the given length.
741+
///
742+
/// [`slice::get_unchecked`]: crate::slice::get_unchecked
743+
/// [`slice::get_unchecked_mut`]: crate::slice::get_unchecked_mut
744+
///
745+
/// # Panics
746+
///
747+
/// Panics if the range would be out of bounds.
748+
///
749+
/// # Examples
750+
///
751+
/// ```
752+
/// #![feature(range_bounds_for_length)]
753+
///
754+
/// let v = [10, 40, 30];
755+
/// assert_eq!(1..2, (1..2).for_length(v.len()));
756+
/// assert_eq!(0..2, (..2).for_length(v.len()));
757+
/// assert_eq!(1..3, (1..).for_length(v.len()));
758+
/// ```
759+
///
760+
/// Panics when [`Index::index`] would panic:
761+
///
762+
/// ```should_panic
763+
/// #![feature(range_bounds_for_length)]
764+
///
765+
/// (2..1).for_length(3);
766+
/// ```
767+
///
768+
/// ```should_panic
769+
/// #![feature(range_bounds_for_length)]
770+
///
771+
/// (1..4).for_length(3);
772+
/// ```
773+
///
774+
/// ```should_panic
775+
/// #![feature(range_bounds_for_length)]
776+
///
777+
/// (1..=usize::MAX).for_length(3);
778+
/// ```
779+
///
780+
/// [`Index::index`]: crate::ops::Index::index
781+
#[track_caller]
782+
#[unstable(feature = "range_bounds_for_length", issue = "76393")]
783+
fn for_length(self, len: usize) -> Range<usize>
784+
where
785+
Self: RangeBounds<usize>,
786+
{
787+
let start: Bound<&usize> = self.start_bound();
788+
let start = match start {
789+
Bound::Included(&start) => start,
790+
Bound::Excluded(start) => {
791+
start.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
792+
}
793+
Bound::Unbounded => 0,
794+
};
795+
796+
let end: Bound<&usize> = self.end_bound();
797+
let end = match end {
798+
Bound::Included(end) => {
799+
end.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
800+
}
801+
Bound::Excluded(&end) => end,
802+
Bound::Unbounded => len,
803+
};
804+
805+
if start > end {
806+
slice_index_order_fail(start, end);
807+
}
808+
if end > len {
809+
slice_end_index_len_fail(end, len);
810+
}
811+
812+
Range { start, end }
813+
}
732814
}
733815

734816
use self::Bound::{Excluded, Included, Unbounded};

library/core/src/slice/index.rs

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Indexing implementations for `[T]`.
22
3-
use crate::ops::{self, Bound, Range, RangeBounds};
3+
use crate::ops;
44
use crate::ptr;
55

66
#[stable(feature = "rust1", since = "1.0.0")]
@@ -37,104 +37,31 @@ fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
3737
#[inline(never)]
3838
#[cold]
3939
#[track_caller]
40-
pub(super) fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
40+
pub(crate) fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
4141
panic!("range end index {} out of range for slice of length {}", index, len);
4242
}
4343

4444
#[inline(never)]
4545
#[cold]
4646
#[track_caller]
47-
pub(super) fn slice_index_order_fail(index: usize, end: usize) -> ! {
47+
pub(crate) fn slice_index_order_fail(index: usize, end: usize) -> ! {
4848
panic!("slice index starts at {} but ends at {}", index, end);
4949
}
5050

5151
#[inline(never)]
5252
#[cold]
5353
#[track_caller]
54-
pub(super) fn slice_start_index_overflow_fail() -> ! {
54+
pub(crate) fn slice_start_index_overflow_fail() -> ! {
5555
panic!("attempted to index slice from after maximum usize");
5656
}
5757

5858
#[inline(never)]
5959
#[cold]
6060
#[track_caller]
61-
pub(super) fn slice_end_index_overflow_fail() -> ! {
61+
pub(crate) fn slice_end_index_overflow_fail() -> ! {
6262
panic!("attempted to index slice up to maximum usize");
6363
}
6464

65-
/// Performs bounds-checking of the given range.
66-
/// The returned [`Range`] is safe to pass to [`get_unchecked`] and [`get_unchecked_mut`]
67-
/// for slices of the given length.
68-
///
69-
/// [`get_unchecked`]: ../../std/primitive.slice.html#method.get_unchecked
70-
/// [`get_unchecked_mut`]: ../../std/primitive.slice.html#method.get_unchecked_mut
71-
///
72-
/// # Panics
73-
///
74-
/// Panics if the range is out of bounds.
75-
///
76-
/// # Examples
77-
///
78-
/// ```
79-
/// #![feature(slice_check_range)]
80-
/// use std::slice;
81-
///
82-
/// let v = [10, 40, 30];
83-
/// assert_eq!(1..2, slice::check_range(v.len(), 1..2));
84-
/// assert_eq!(0..2, slice::check_range(v.len(), ..2));
85-
/// assert_eq!(1..3, slice::check_range(v.len(), 1..));
86-
/// ```
87-
///
88-
/// Panics when [`Index::index`] would panic:
89-
///
90-
/// ```should_panic
91-
/// #![feature(slice_check_range)]
92-
///
93-
/// std::slice::check_range(3, 2..1);
94-
/// ```
95-
///
96-
/// ```should_panic
97-
/// #![feature(slice_check_range)]
98-
///
99-
/// std::slice::check_range(3, 1..4);
100-
/// ```
101-
///
102-
/// ```should_panic
103-
/// #![feature(slice_check_range)]
104-
///
105-
/// std::slice::check_range(3, 1..=usize::MAX);
106-
/// ```
107-
///
108-
/// [`Index::index`]: ops::Index::index
109-
#[track_caller]
110-
#[unstable(feature = "slice_check_range", issue = "76393")]
111-
pub fn check_range<R: RangeBounds<usize>>(len: usize, range: R) -> Range<usize> {
112-
let start = match range.start_bound() {
113-
Bound::Included(&start) => start,
114-
Bound::Excluded(start) => {
115-
start.checked_add(1).unwrap_or_else(|| slice_start_index_overflow_fail())
116-
}
117-
Bound::Unbounded => 0,
118-
};
119-
120-
let end = match range.end_bound() {
121-
Bound::Included(end) => {
122-
end.checked_add(1).unwrap_or_else(|| slice_end_index_overflow_fail())
123-
}
124-
Bound::Excluded(&end) => end,
125-
Bound::Unbounded => len,
126-
};
127-
128-
if start > end {
129-
slice_index_order_fail(start, end);
130-
}
131-
if end > len {
132-
slice_end_index_len_fail(end, len);
133-
}
134-
135-
Range { start, end }
136-
}
137-
13865
mod private_slice_index {
13966
use super::ops;
14067
#[stable(feature = "slice_get_slice", since = "1.28.0")]

library/core/src/slice/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub mod memchr;
2929

3030
mod ascii;
3131
mod cmp;
32-
mod index;
32+
pub(crate) mod index;
3333
mod iter;
3434
mod raw;
3535
mod rotate;
@@ -75,9 +75,6 @@ pub use sort::heapsort;
7575
#[stable(feature = "slice_get_slice", since = "1.28.0")]
7676
pub use index::SliceIndex;
7777

78-
#[unstable(feature = "slice_check_range", issue = "76393")]
79-
pub use index::check_range;
80-
8178
#[lang = "slice"]
8279
#[cfg(not(test))]
8380
impl<T> [T] {
@@ -2758,7 +2755,7 @@ impl<T> [T] {
27582755
where
27592756
T: Copy,
27602757
{
2761-
let Range { start: src_start, end: src_end } = check_range(self.len(), src);
2758+
let Range { start: src_start, end: src_end } = src.for_length(self.len());
27622759
let count = src_end - src_start;
27632760
assert!(dest <= self.len() - count, "dest is out of bounds");
27642761
// SAFETY: the conditions for `ptr::copy` have all been checked above,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# `range_bounds_for_length`
2+
3+
The tracking issue for this feature is: [#76393]
4+
5+
------------------------
6+
7+
This adds [`RangeBounds::for_length`].
8+
9+
[#76393]: https://github.com/rust-lang/rust/issues/76393
10+
[`RangeBounds::for_length`]: https://doc.rust-lang.org/nightly/std/ops/trait.RangeBounds.html#method.for_length

src/doc/unstable-book/src/library-features/slice-check-range.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)