diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index eb899468193..0c267cbc106 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -1063,7 +1063,7 @@ impl VecDeque { where R: RangeBounds, { - let Range { start, end } = range.assert_len(self.len()); + let Range { start, end } = range.ensure_subset_of(..self.len()); let tail = self.wrap_add(self.tail, start); let head = self.wrap_add(self.tail, end); (tail, head) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 99c42a4ba44..71b4883aca2 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -115,7 +115,7 @@ #![feature(or_patterns)] #![feature(pattern)] #![feature(ptr_internals)] -#![feature(range_bounds_assert_len)] +#![feature(range_bounds_ensure_subset_of)] #![feature(rustc_attrs)] #![feature(receiver_trait)] #![cfg_attr(bootstrap, feature(min_const_generics))] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 3218b3535c9..3ab5ca4f566 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1510,14 +1510,14 @@ 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 Range { start, end } = range.assert_len(self.len()); + let Range { start, end } = range.ensure_subset_of(..self.len()); 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 _; - // SAFETY: `assert_len` and `is_char_boundary` do the appropriate bounds checks. + // SAFETY: `ensure_subset_of` 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 } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index b40c1a8c57a..5c20f382224 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1650,7 +1650,7 @@ impl Vec { // the hole, and the vector length is restored to the new length. // let len = self.len(); - let Range { start, end } = range.assert_len(len); + let Range { start, end } = range.ensure_subset_of(..len); unsafe { // set self.vec length's to start, to be safe in case Drain is leaked diff --git a/library/core/src/ops/range.rs b/library/core/src/ops/range.rs index 0571dc74b9a..7a0dd5a8f0f 100644 --- a/library/core/src/ops/range.rs +++ b/library/core/src/ops/range.rs @@ -766,8 +766,15 @@ pub trait RangeBounds { /// Performs bounds-checking of this range. /// + /// This method is similar to [`Index::index`] for slices, but it returns a + /// [`Range`] equivalent to this range. You can use this method to turn any + /// range into `start` and `end` values. + /// + /// The given range is the range of the slice to use for bounds-checking. It + /// should be a [`RangeTo`] range that ends at the length of the slice. + /// /// The returned [`Range`] is safe to pass to [`slice::get_unchecked`] and - /// [`slice::get_unchecked_mut`] for slices of the given length. + /// [`slice::get_unchecked_mut`] for slices with the given range. /// /// [`slice::get_unchecked`]: ../../std/primitive.slice.html#method.get_unchecked /// [`slice::get_unchecked_mut`]: ../../std/primitive.slice.html#method.get_unchecked_mut @@ -779,49 +786,51 @@ pub trait RangeBounds { /// # Examples /// /// ``` - /// #![feature(range_bounds_assert_len)] + /// #![feature(range_bounds_ensure_subset_of)] /// /// use std::ops::RangeBounds; /// /// let v = [10, 40, 30]; - /// assert_eq!(1..2, (1..2).assert_len(v.len())); - /// assert_eq!(0..2, (..2).assert_len(v.len())); - /// assert_eq!(1..3, (1..).assert_len(v.len())); + /// assert_eq!(1..2, (1..2).ensure_subset_of(..v.len())); + /// assert_eq!(0..2, (..2).ensure_subset_of(..v.len())); + /// assert_eq!(1..3, (1..).ensure_subset_of(..v.len())); /// ``` /// /// Panics when [`Index::index`] would panic: /// /// ```should_panic - /// #![feature(range_bounds_assert_len)] + /// #![feature(range_bounds_ensure_subset_of)] /// /// use std::ops::RangeBounds; /// - /// (2..1).assert_len(3); + /// (2..1).ensure_subset_of(..3); /// ``` /// /// ```should_panic - /// #![feature(range_bounds_assert_len)] + /// #![feature(range_bounds_ensure_subset_of)] /// /// use std::ops::RangeBounds; /// - /// (1..4).assert_len(3); + /// (1..4).ensure_subset_of(..3); /// ``` /// /// ```should_panic - /// #![feature(range_bounds_assert_len)] + /// #![feature(range_bounds_ensure_subset_of)] /// /// use std::ops::RangeBounds; /// - /// (1..=usize::MAX).assert_len(3); + /// (1..=usize::MAX).ensure_subset_of(..3); /// ``` /// /// [`Index::index`]: crate::ops::Index::index #[track_caller] - #[unstable(feature = "range_bounds_assert_len", issue = "76393")] - fn assert_len(self, len: usize) -> Range + #[unstable(feature = "range_bounds_ensure_subset_of", issue = "76393")] + fn ensure_subset_of(self, range: RangeTo) -> Range where Self: RangeBounds, { + let len = range.end; + let start: Bound<&usize> = self.start_bound(); let start = match start { Bound::Included(&start) => start, diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 19a3b45e568..90351be6929 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -3052,7 +3052,7 @@ impl [T] { where T: Copy, { - let Range { start: src_start, end: src_end } = src.assert_len(self.len()); + let Range { start: src_start, end: src_end } = src.ensure_subset_of(..self.len()); let count = src_end - src_start; assert!(dest <= self.len() - count, "dest is out of bounds"); // SAFETY: the conditions for `ptr::copy` have all been checked above, diff --git a/src/doc/unstable-book/src/library-features/range-bounds-assert-len.md b/src/doc/unstable-book/src/library-features/range-bounds-assert-len.md deleted file mode 100644 index 0e95d5ded92..00000000000 --- a/src/doc/unstable-book/src/library-features/range-bounds-assert-len.md +++ /dev/null @@ -1,10 +0,0 @@ -# `range_bounds_assert_len` - -The tracking issue for this feature is: [#76393] - ------------------------- - -This adds [`RangeBounds::assert_len`]. - -[#76393]: https://github.com/rust-lang/rust/issues/76393 -[`RangeBounds::assert_len`]: https://doc.rust-lang.org/nightly/std/ops/trait.RangeBounds.html#method.assert_len diff --git a/src/doc/unstable-book/src/library-features/range-bounds-ensure-subset-of.md b/src/doc/unstable-book/src/library-features/range-bounds-ensure-subset-of.md new file mode 100644 index 00000000000..ea3f01ff5f9 --- /dev/null +++ b/src/doc/unstable-book/src/library-features/range-bounds-ensure-subset-of.md @@ -0,0 +1,10 @@ +# `range_bounds_ensure_subset_of` + +The tracking issue for this feature is: [#76393] + +------------------------ + +This adds [`RangeBounds::ensure_subset_of`]. + +[#76393]: https://github.com/rust-lang/rust/issues/76393 +[`RangeBounds::ensure_subset_of`]: https://doc.rust-lang.org/nightly/std/ops/trait.RangeBounds.html#method.ensure_subset_of